Updating the MarkerImage sprite origin point in a Google Maps marker (v3)
I want to dynamically update the "origin" point of a sprite generated by the MarkerImage constructor in Google Maps API v3, and am wondering if that's possible without generating a new MarkerImage entirely.
Here's some code:
// Marker options
var markerOptions = {
icon: new google.maps.MarkerImage('../images/content/marker.png',
new google.maps.Size(88,88),
new google.maps.Point(0,0),
new google.maps.Point(44,88)
),
position: position,
map: map
}
var marker = new google.maps.Marker(markerOptions);
Can I do something along the lines of marker.setPoint(0,10) or do I need to create a new M开发者_高级运维arkerImage just to set the sprite origin again?
When the MarkerImage is draw on the map the quickest way to do that is :
marker.getIcon().origin = new google.maps.Point(0, 10);
marker.setIcon(marker.getIcon()); //re-draw the MarkerIcon
You can set the origin property of the marker's icon. In your example:
marker.getIcon().origin = new google.maps.Point(0, 10);
The following also works:
marker.icon.origin = new google.maps.Point(0, 10);
There is no way (at least according to the API reference) to alter the properties of a MarkerImage after it has been created. You can create a new MarkerImage and call marker.setIcon()
.
精彩评论