0

这是我的代码:

    <!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        margin: 0;
        padding: 0;
        height: 100%;
      }
    </style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;

function initialize() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

function displayRoute() {

    var start = new google.maps.LatLng(28.694004, 77.110291);
    var end = new google.maps.LatLng(28.72082, 77.107241);

    var directionsDisplay = new google.maps.DirectionsRenderer();// also, constructor can get "DirectionsRendererOptions" object
    directionsDisplay.setMap(map); // map should be already initialized.

    var request = {
        origin : start,
        destination : end,
        travelMode : google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}
   </script>
  </head>
  <body onload="displayRoute">
    <div id="map-canvas"></div>
  </body>
</html>

为什么这不起作用???

我只是想不让用户输入两个坐标,我想预先定义这些坐标,然后只绘制一张带有路线的地图。理想情况下,我只想要一条线,我不想要方向或任何东西。

底线:我只是想突出显示两个给定点之间的道路。

4

1 回答 1

0
  1. 您需要创建 DirectionsService 的实例: var map; var directionsService = new google.maps.DirectionsService(); onload 也应该是onload="displayRoute()"
于 2013-09-05T19:33:23.357 回答