我有一个脚本可以将标记一个一个地添加到 map 上
var marker = new google.maps.Marker({
position: new google.maps.LatLng(51,-117)
});
marker.setIcon(getIconFile(initialIconId));
markers.push(new Array(id,marker)); // id, marker
marker.setMap(map);
稍后在脚本中,当我按下自定义按钮时,我希望能够更改标记图标。所以我通过标记数组中的 id 获取标记并调用:
markers[markerIndex].setIcon(getIconFile(newIconId)); // which returns a string url of the icon
但是我收到:TypeError: markers[markerId].setIcon is not a function
我打印了 markerId 并且它是有效的,我还打印了索引标记 markers[markerId] 的结果,它返回了一个标记对象。我没有其他方法来调试这个我迷路了!
谢谢
最佳答案
看起来您正在将一个数组插入标记中,而不仅仅是一个元素。
为什么不呢:
markers = []
markers.push(marker)
markers[markerIndex].setIcon(getIconFile(newIconId));
或者如果你坚持要插入一个数组:
markers.push(new Array(id,marker));
markers[markerIndex][1].setIcon(getIconFile(newIconId));
关于javascript - Google V3 - 引用标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14160165/