如何在添加后动态更改Google地图标记 [英] How to dynamically change a google maps marker after its added
问题描述
使用google maps API(v3),我可以使用自定义图标成功添加标记数组到图。
每个标记代表一个目标,我想当相应的目的地名称被鼠标移动到页面上的其他位置时,更改图标图像。
我可以添加事件侦听器来拾取鼠标,我知道我可以使用marker.setIcon(imgurl)来改变图标,但我无法弄清楚如何引用特定的标记进行更改?
我已经读过,我可以在定义标记时添加一个id,但是我无法弄清楚如何使用这个方法是与marker.seticon结合来更新特定的标记。
感谢您的帮助。
如果您有每个目标的ID号,请保留一组标记例如,
var myMarkers = []
for(var i = 0; i< dest inations.length; i ++){
myMarkers [destination [i] .id] = new google.maps.Marker(markerOpts)
}
和其他地方的目的地链接:
onclick = function(){
myMarkers [destinationID] .setIcon(otherIcon)
}
Using the google maps API (v3) I can successfully add an array of markers to the map using custom icons.
Each marker represents a destination and I want to change the icon image when the respective destination name is mouse'd over elsewhere on the page.
I can add the event listener to pick up the mouse over, and I know I can use marker.setIcon("imgurl") to change the icon, however what I can't figure out is how to reference the specific marker to be changed?
I've read that I can add an "id" when defining the marker, however I can't figure out how to use this is conjunction with marker.seticon to update that specific marker.
Thanks for your help.
If you have ID numbers for each destination, keep an array of the markers indexed by your destination.
For example,
var myMarkers = []
for(var i = 0; i < destinations.length; i++) {
myMarkers[destination[i].id] = new google.maps.Marker(markerOpts)
}
and in your destination links elsewhere:
onclick = function() {
myMarkers[destinationID].setIcon(otherIcon)
}
这篇关于如何在添加后动态更改Google地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!