Search code examples
google-mapsgoogle-maps-api-3google-maps-markersdriving-directionsmap-directions

Changing titles of endpoint markers in a Google Maps route


I have a simple test webpage, which displays driving directions:

Mozilla screenshot

It works well, but my problem is that the start and end markers of the shown route have the meaningless titles "A" and "B".

I would like to use ReverseGeocoding to fetch the titles of the 2 route endpoints.

So I have prepared the following code and the addresses are fetched correctly and are printed out using 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>

However my problem is that I don't know how to access the 2 endpoint markers of the route and change their titles from "A" and "B" to the address strings that I have fetched.

The Google Maps JavaScript API v3 reference for DirectionsRenderer doesn't list any obvious hooks for that (or should I somehow use the infoWindow property here? But why isn't it an array since I have 2 endpoints?)...

UPDATE:

Here is updated code after Emmanuel's suggestion (thanks!)

It works, but I am not happy with it because the endpoint titles are only displayed, when the user hovers the mouse over the markers - and I don't have a mouse in my target environment. I wonder if there is some better option to make the endpoint addresses permanently visible at the map?

<!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>

Solution

  • I don't know how to access those markers. But I have an alternative; you can suppress those markers and set your own. Then you can make them how ever you want.

    <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>