Google会映射目的地别名

时间:2013-08-23 00:56:36

标签: google-maps-api-3 driving-directions

我在http://www.no1hastings.check.com.au/directions.html有一个页面,访问者可以从任何地方到固定点获取路线。 Google地理编码器识别的目的地是街道地址(1 Morwong Drive),但我希望它显示那里的建筑物名称(黑斯廷斯街1号),Google无法识别。

有没有办法将街道地址设置为目的地,但在显示结果时将其别名为建筑物名称?

1 个答案:

答案 0 :(得分:1)

一个选项是在将响应中的字符串发送到DirectionsRenderer之前修改它:

function calcRoute() {
  var request = {
    origin: 'Brisbane QLD Australia Australia',
    //  origin: 'Brisbane Qld, Australia',
    destination: '1 Morwong Drive, Noosa Heads Qld, Australia',
    //  waypoints:[{location: 'Bourke, NSW'}, {location: 'Broken Hill, NSW'}],
    travelMode: google.maps.DirectionsTravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      var route = response.routes[0];
      var lastleg = route.legs[route.legs.length-1];
      response.routes[0].legs[route.legs.length-1].end_address = 'No. 1 in Hastings Street';
      directionsDisplay.setDirections(response);
    } else alert("Directions request failed: "+status);
  });
}

要处理directionsRenderer更改方向的情况(即拖动原点),您可以执行类似这样的操作(其中changeEnd是全局并设置为false),请注意,如果使用此方法,则会产生不希望的结果用户拖动目的地(您可能希望阻止:Google maps api v3 Two markers one fixed one dragged):

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
  computeTotalDistance(directionsDisplay.directions);
   if (!changeEnd) {
     var response = directionsDisplay.getDirections();
     var route = response.routes[0];
     var lastleg = route.legs[route.legs.length-1];
     response.routes[0].legs[route.legs.length-1].end_address = 'No. 1 in Hastings Street';
     changeEnd = true;
     directionsDisplay.setDirections(response);
   } else changeEnd = false;
});