Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the color of the dotted line on returned Google map directions

Using Google directions service I'm getting transit results between two locations and displaying the result on the map. I want to change the color of the line color between the two locations. I'm using google.maps.Polyline to change the main line color but there are sections where the line is dotted (to show where you have to walk) but that doesn't change to the same color that the main line is. What can I do to change the dotted lines color?

/* change line color */
var polylineOptionsActual = new google.maps.Polyline({
  strokeColor: '#9f98ff',
  strokeWeight: 5
});

function initialize() {
  /* create map */
  var mapOptions = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  /* directions */
  var rendererOptions = { 
    map: map, 
    suppressMarkers: true,
    polylineOptions: polylineOptionsActual
  } 
  directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
  directionsDisplay.setPanel(document.getElementById('directions-results'));
}

function getDirections() {
  /* directions request */
  var request = {
    origin: document.getElementById('from-input').value,
    destination: document.getElementById('to-input').value,
    travelMode: google.maps.TravelMode.TRANSIT
  };

  /* display directions */
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}
like image 860
Scott Avatar asked Oct 18 '25 03:10

Scott


1 Answers

Though it's undocumented by G, the google.maps.DirectionsRenderer does expose its polylines via its property b.polylines which is an array of google.maps.Polyline instances. So, if we search through them, we find that only the ones which have dotted lines have an 'icons' property, which we can alter via google.maps.Polyline.setOptions() Include the following in global scope of your code:

//iconSequence must be a single instance of google.maps.IconSequence object
google.maps.DirectionsRenderer.prototype.setDottedPolylineOptions = function (iconSequence) {
     //need a reference to the current 'this' object
    var obj = this;
     //in case this DirectionsRenderer's directions were just set an instant ago,
     //need a slight delay before we may access the b.polylines property of this object
    window.setTimeout(function () {
        var i,
            lines = obj.b.polylines,
            len = lines.length;
        for (i = 0; i < len; i++) {
            if (lines[i].icons) {
                lines[i].setOptions(
                    {
                        icons: [iconSequence]
                    }
                );
            }
        }
    },1);
};

And then you can within your code do:

var iconSequence = {
    icon: {
        fillColor: 'red', //or hexadecimal color such as: '#FF0000'
        fillOpacity: 0.8,
        scale: 3,
        strokeColor: 'blue',
        strokeWeight: 1,
        strokeOpacity: 0.8,
        path: google.maps.SymbolPath.CIRCLE
    },
    repeat: '10px'
};
directionsDisplay.setDottedPolylineOptions(iconSequence);

I should note that the above should be done AFTER setting the directionsDisplay's directions.

Here's a fiddle: http://jsfiddle.net/Wx9XV/

like image 189
astupidname Avatar answered Oct 22 '25 04:10

astupidname



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!