Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API 3 - Custom marker color for default (dot) marker

I've seen lots of other questions similar to this (here, here and here), but they all have accepted answers that don't solve my problem. The best solution I have found to the problem is the StyledMarker library, which does let you define custom colours for markers, but I can't get it to use the default marker (the one you get when you do a google maps search - with a dot in the middle), it just seems to provide markers with a letter in, or with a special icon.

like image 519
jackocnr Avatar asked Aug 17 '11 15:08

jackocnr


People also ask

What do the colors mean on google maps default?

Traffic colorsGreen: No traffic delays. Orange: Medium amount of traffic. Red: Traffic delays. The darker the red, the slower the speed of traffic on the road.

What is the color coding on google maps?

Traffic. Green: If your maps have the traffic layer, this color means there should be no delays due to traffic. Orange: If your maps have the traffic layer or selected Directions, this color means that there is some traffic, and therefore it will affect your route. Red: There are two types of red: normal and dark red.


2 Answers

You can dynamically request icon images from the Google charts api with the urls:

http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|FE7569 

Which looks like this: default color the image is 21x34 pixels and the pin tip is at position (10, 34)

And you'll also want a separate shadow image (so that it doesn't overlap nearby icons):

http://chart.apis.google.com/chart?chst=d_map_pin_shadow 

Which looks like this: enter image description here the image is 40x37 pixels and the pin tip is at position (12, 35)

When you construct your MarkerImages you need to set the size and anchor points accordingly:

    var pinColor = "FE7569";     var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor,         new google.maps.Size(21, 34),         new google.maps.Point(0,0),         new google.maps.Point(10, 34));     var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",         new google.maps.Size(40, 37),         new google.maps.Point(0, 0),         new google.maps.Point(12, 35)); 

You can then add the marker to your map with:

        var marker = new google.maps.Marker({                 position: new google.maps.LatLng(0,0),                  map: map,                 icon: pinImage,                 shadow: pinShadow             }); 

Simply replace "FE7569" with the color code you're after. Eg: default colorgreenyellow

Credit due to Jack B Nimble for the inspiration ;)

like image 75
matt burns Avatar answered Sep 20 '22 11:09

matt burns


If you use Google Maps API v3 you can use setIcon e.g.

marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png') 

Or as part of marker init:

marker = new google.maps.Marker({     icon: 'http://...' }); 

Other colours:

  • Blue marker
  • enter image description hereRed marker
  • enter image description herePurple marker
  • enter image description hereYellow marker
  • enter image description hereGreen marker

Use the following piece of code to update default markers with different colors.

(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE) 
like image 33
Mike Avatar answered Sep 19 '22 11:09

Mike