2013-10-29 32 views
6

複数のマーカーをプロットしたり、2点間のルートをプロットすることは可能ですか?複数のマーカーをプロットする方法と、2点間のルートをプロットする方法はありますか?

Iveは別にそれに成功しました。しかし、それはどのように同じ地図で行うことができますか?

私が2つを組み合わせると、ルートプロットは完了しませんが、マーカーはプロットされています。 IVEは、これまで行って何

は以下の通りです:

var directionsDisplay; 
    var directionsService = new google.maps.DirectionsService(); 
    var map; 

    var locations = [ 
     ['Bondi Beach', -33.890542, 151.274856, 4], 
     ['Coogee Beach', -33.923036, 151.259052, 5], 
     ['Cronulla Beach', -34.028249, 151.157507, 3], 
     ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
     ['Maroubra Beach', -33.950198, 151.259302, 1] 
    ]; 


    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
      map: map 
     }); 

     google.maps.event.addListener(marker, 'click', (function (marker, i) { 
      return function() { 
       infowindow.setContent(locations[i][0]); 
       infowindow.open(map, marker); 
      } 
     })(marker, i)); 
    } 

    directionsDisplay = new google.maps.DirectionsRenderer(); 

    directionsDisplay.setMap(map); 

    var start = new google.maps.LatLng(-33.890542, 151.274856); 
    var end = new google.maps.LatLng(-34.028249, 151.157507); 
    var request = { 
     origin: start, 
     destination: end, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 
    directionsService.route(request, function (response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay.setDirections(response); 
     } 
    }); 

    map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 10, 
     center: new google.maps.LatLng(-33.92, 151.25), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

HTML:

<div id="map-canvas"></div> 

スクリプト:あなたはあなたがそれを使用する前に、マップ変数を初期化する必要が

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 

答えて

10

あなたマーカーと方向。マップを表示するdivの名前が "map-canvas"の場合、google.maps.Mapコンストラクタでその名前を使用する必要があります。

var directionsDisplay; 
var directionsService = new google.maps.DirectionsService(); 
var map; 

var locations = [ 
    ['Bondi Beach', -33.890542, 151.274856, 4], 
    ['Coogee Beach', -33.923036, 151.259052, 5], 
    ['Cronulla Beach', -34.028249, 151.157507, 3], 
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
    ['Maroubra Beach', -33.950198, 151.259302, 1] 
]; 


var infowindow = new google.maps.InfoWindow(); 

var marker, i; 

map = new google.maps.Map(document.getElementById('map-canvas'), { 
    zoom: 10, 
    center: new google.maps.LatLng(-33.92, 151.25), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

for (i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map 
    }); 

    google.maps.event.addListener(marker, 'click', (function (marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
    })(marker, i)); 
} 

directionsDisplay = new google.maps.DirectionsRenderer(); 

directionsDisplay.setMap(map); 

var start = new google.maps.LatLng(-33.890542, 151.274856); 
var end = new google.maps.LatLng(-34.028249, 151.157507); 
var request = { 
    origin: start, 
    destination: end, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 
directionsService.route(request, function (response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
    } 
}); 

ページのレンダリングが完了したら、マップコードを実行する必要があります。私はルートに複数の停止を持っているし、ポイントの開始と停止

working example

+0

恐ろしい男...おかげで...私の一日保存.. :) – phpdev

+0

@geocodezipはと場所配列最初のポイントから取り上げるべきです開始点と終了点としての最後の点は可能です!!!! –

関連する問題