添加我的Google地图的路线

时间:2015-08-07 10:55:39

标签: google-maps

我使用以下代码在地图上查看两个标记。

<script
src="http://maps.google.com/maps?file=api&v=2"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
//globals
var bounds = new GLatLngBounds();


function initMap() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
var title = "Map";
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
var point = new GLatLng(&P1_START_LOCATION.);
var marker = new GMarker(point);
map.addOverlay(marker);

var point2 = new GLatLng(&P1_END_LOCATION.);
var marker2 = new GMarker(point2);
map.addOverlay(marker2);

bounds.extend(new GLatLng( &P1_SOUTH., &P1_WEST. )); 
bounds.extend(new GLatLng( &P1_NORTH., &P1_EAST. )); 
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));



}
}
//]]>
</script> 

这里的标记和边界值来自方向API。我现在想知道如何添加两者之间的蓝色方向线,我有点卡住了。我尝试添加以下内容:

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

 directionsDisplay = new google.maps.DirectionsRenderer();

      var request = {
          origin:point,
          destination:point2,
          travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });

        directionsDisplay.setMap(map);

我没有得到任何结果,说实话,这有点盲目。任何帮助或指示正确的方向将不胜感激!

编辑:这是我现在的代码 - 我有一个带有两个标记的地图,但我还没有绘制方向。

<script
src="http://maps.google.com/maps?file=api&v=3"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
//globals
var bounds = new google.maps.LatLngBounds();
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

    var startPoint = new google.maps.LatLng(37.7699298, -122.4469157);
var endPoint = new google.maps.LatLng(37.7683909618184, -122.51089453697205);


function initMap() {
if (google.maps.BrowserIsCompatible()) {
var map = new google.maps.Map2(document.getElementById("map"));
var title = "Map";
map.addControl(new google.maps.SmallMapControl());
map.addControl(new google.maps.MapTypeControl());
var point = new google.maps.LatLng(&P1_START_LOCATION.);
var marker = new google.maps.Marker(point);
map.addOverlay(marker);

var point2 = new google.maps.LatLng(&P1_END_LOCATION.);
var marker2 = new google.maps.Marker(point2);
map.addOverlay(marker2);

bounds.extend(new google.maps.LatLng( &P1_SOUTH., &P1_WEST. )); 
bounds.extend(new google.maps.LatLng( &P1_NORTH., &P1_EAST. )); 

map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

    directionsDisplay = new google.maps.DirectionsRenderer();

  var request = {
      origin:point,
      destination:point2,
      travelMode: google.maps.TravelMode.DRIVING
  };

  directionsService.route(request) 
  directionsDisplay.setDirections(response);

  directionsDisplay.setMap(map);

}
}
//]]>
</script> 

1 个答案:

答案 0 :(得分:0)

嗯,我使用了以下内容:

 <head><meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps JavaScript API v3 Example: Directions Complex</title> 
   <script type="text/javascript" 
           src="http://maps.google.com/maps/api/js?sensor=false"></script></head>

<body>

<div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

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

   var myOptions = {
     zoom:7,
     mapTypeId: google.maps.MapTypeId.ROADMAP
   }

   var map = new google.maps.Map(document.getElementById("map"), myOptions);
   directionsDisplay.setMap(map);

   var request = {
       origin: '&P1_ORIGIN.', 
       destination: '&P1_DESTINATION.',
       travelMode: google.maps.DirectionsTravelMode.DRIVING
   };

   directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {


         directionsDisplay.setDirections(response);
      }
   });
   </script> 
</body>