0

我有一个简单的测试网页,显示行车路线:

Mozilla 截图

它运作良好,但我的问题是所示路线的开始和结束标记具有无意义的标题“A”和“B”。

我想使用ReverseGeocoding来获取 2 个路由端点的标题。

所以我准备了以下代码,地址被正确提取并打印出来使用console.log

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="http://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer({});
  map = new google.maps.Map(document.getElementById('map-canvas'), null);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = new google.maps.LatLng(51.470907, 7.225558);
  var end = new google.maps.LatLng(52.435293, 10.736883);

  var startGeocoder = new google.maps.Geocoder();
  var endGeocoder = new google.maps.Geocoder();

  startGeocoder.geocode({'latLng': start}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results[1]) {
      console.log('START: ' + results[1].formatted_address);
      //infowindow.setContent(results[1].formatted_address);
      //infowindow.open(map, marker);
    }
  });

  endGeocoder.geocode({'latLng': end}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results[1]) {
      console.log('END: ' + results[1].formatted_address);
      //infowindow.setContent(results[1].formatted_address);
      //infowindow.open(map, marker);
    }
  });

  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);
    }
  });
}

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

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

但是我的问题是我不知道如何访问路线的 2 个端点标记并将它们的标题从“A”和“B”更改为我获取的地址字符串。

DirectionsRenderer的 Google Maps JavaScript API v3 参考没有列出任何明显的钩子(或者我应该在infoWindow这里以某种方式使用该属性?但是为什么它不是一个数组,因为我有 2 个端点?)...

更新:

这是伊曼纽尔建议后的更新代码(谢谢!)

它可以工作,但我对此并不满意,因为仅当用户将鼠标悬停在标记上时才会显示端点标题 - 而我的目标环境中没有鼠标。我想知道是否有更好的选择可以使端点地址在地图上永久可见?

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

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
  map = new google.maps.Map(document.getElementById('map-canvas'), null);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = new google.maps.LatLng(51.470907, 7.225558);
  var end = new google.maps.LatLng(52.435293, 10.736883);

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

  var directionsService = new google.maps.DirectionsService();
  directionsService.route(request, function(response, status) {
    if (status != google.maps.DirectionsStatus.OK) 
      return;

    directionsDisplay.setDirections(response);

    // add start and end markers
    var start = response.mc.origin;
    var startMarker = new google.maps.Marker({
      position: start,
      map: map
    });

    var startGeocoder = new google.maps.Geocoder();
    startGeocoder.geocode({'latLng': start}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK && results[1]) {
        startMarker.setOptions({title: 'START: ' + results[1].formatted_address});
      }
    });

    var end = response.mc.destination;
    var endMarker = new google.maps.Marker({
      position: end,
      map: map
    });

    var endGeocoder = new google.maps.Geocoder();
    endGeocoder.geocode({'latLng': end}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK && results[1]) {
        endMarker.setOptions({title: 'END: ' + results[1].formatted_address});
      }
    });
  });
}

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

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>
4

1 回答 1

2

我不知道如何访问这些标记。但我有一个选择;您可以抑制这些标记并设置自己的标记。然后,您可以随心所欲地制作它们。

<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var startMarker;
var endMarker;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
  map = new google.maps.Map(document.getElementById('map-canvas'), null);
  directionsDisplay.setMap(map);
  calcRoute();
}

function calcRoute() {
  var start = new google.maps.LatLng(51.470907, 7.225558);
  var end = new google.maps.LatLng(52.435293, 10.736883);

  var startGeocoder = new google.maps.Geocoder();
  var endGeocoder = new google.maps.Geocoder();

  startGeocoder.geocode({'latLng': start}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results[1]) {
      startMarker.setOptions({title: 'START: ' + results[1].formatted_address});
    }
  });

  endGeocoder.geocode({'latLng': end}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results[1]) {
      endMarker.setOptions({title: 'END: ' + results[1].formatted_address});
    }
  });

  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);
      // add start and end markers
      startMarker = new google.maps.Marker({
        position: response.mc.origin,
        icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png',
        map: map
      });
      endMarker = new google.maps.Marker({
        position: response.mc.destination,
        map: map
      });
    }
  });
}

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

</script>
于 2014-10-20T10:03:44.670 回答