Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API v3: How do I dynamically change the marker icon?

Using Google Maps API v3, how do I programmatically change the marker icon?

What I would like to do is, when someone hovers over a link - to have the corresponding marker icon on the map change colors to denote the marker in question.

Essentially, the same function as what Roost does.

When you hover over a home listing on the left, the corresponding marker on the right changes color

like image 217
TedK Avatar asked Dec 21 '09 16:12

TedK


People also ask

How do you customize a marker in maps?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I change the color of a Google Marker?

To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.


2 Answers

Call the marker.setIcon('newImage.png')... Look here for the docs.

Are you asking about the actual way to do it? You could just create each div, and a add a mouseover and mouseout listener that would change the icon and back for the markers.

like image 83
Sudhir Jonathan Avatar answered Sep 28 '22 20:09

Sudhir Jonathan


You can also use a circle as a marker icon, for example:

var oMarker = new google.maps.Marker({     position: latLng,     sName: "Marker Name",     map: map,     icon: {         path: google.maps.SymbolPath.CIRCLE,         scale: 8.5,         fillColor: "#F00",         fillOpacity: 0.4,         strokeWeight: 0.4     }, }); 

and then, if you want to change the marker dynamically (like on mouseover), you can, for example:

oMarker.setIcon({             path: google.maps.SymbolPath.CIRCLE,             scale: 10,             fillColor: "#00F",             fillOpacity: 0.8,             strokeWeight: 1         }) 
like image 24
Roman Avatar answered Sep 28 '22 21:09

Roman