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>

なぜこれが機能しないのですか?

ユーザーに2つの座標を入力させないようにしているだけです。それらを事前に定義し、ルートが描かれた地図を作成したいだけです。理想的には、線だけが欲しいだけで、道順などは必要ありません。

結論: 与えられた 2 点間の道路を強調表示したいだけです。

4

1 に答える 1

0
  1. DirectionsService のインスタンスを作成する必要があります var map; var directionsService = new google.maps.DirectionsService();onload="displayRoute()"
于 2013-09-05T19:33:23.357 に答える