Google maps directions destination alias

797 Views Asked by At

I have a page at http://www.no1hastings.check.com.au/directions.html where visitors can get directions from anywhere to a fixed point. The destination that the Google geocoder recognises is a street address (1 Morwong Drive) but I would like it to display the name of the building there (No. 1 in Hastings Street) which Google doesn't recognise.

Is there way to set the street address as the destination but alias it to the building name when the result is displayed?

1

There are 1 best solutions below

3
On

One option would be to modify the string in the response before sending it to the 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);
  });
}

To handle the case where the directions are changed by the directionsRenderer (i.e. for dragging the origin), you could do something like this (where changeEnd is a global and set to false), note that it will have undesireable results if the user drags the destination (you may want to prevent that: 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;
});