javascript Google Maps Api Marker setOptions() 更改图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13916371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps Api Marker setOptions() change icon
提问by geocodezip
I want to change a marker icon when selected by a drawed polygon. In addMarker() the markers are parsed from JSON data and push into allMarkers array. The icon is red when not selected and would become white when selected.
当被绘制的多边形选中时,我想更改标记图标。在 addMarker() 中,标记从 JSON 数据中解析并推送到 allMarkers 数组中。未选中时图标为红色,选中时变为白色。
function addMarker(lat,lng,i){
var myLatlng = new google.maps.LatLng(lat,lng);
var marker = new google.maps.Marker({
position: myLatlng,
icon: "http://labs.google.com/ridefinder/images/mm_20_red.png",
map: map
});
marker.shapeId = '0';
allMarkers.push(marker);
}
function selectMarkersInPoly() {
alert(allMarkers.length)
for (var i=0; i < createdShapes.length; i++) {
for (var j=0; j < allMarkers.length; j++){
var latlong = allMarkers[j].getPosition();
if(google.maps.geometry.poly.containsLocation(latlong, createdShapes[i]) == true) {
allMarkers[j].shapeId = createdShapes[i].id;
allMarkers[j].setOptions({
icon : "http://labs.google.com/ridefinder/images/mm_20_white.png"
});
}
}
}
}
What is wrong with selectMarkersInPoly() ??? Thank you for your help?
selectMarkersInPoly() 有什么问题???谢谢您的帮助?
回答by geocodezip
This is not the correct syntax for a javascript anonymous object:
这不是 javascript 匿名对象的正确语法:
allMarkers[j].setOptions({
icon = "http://labs.google.com/ridefinder/images/mm_20_white.png"
});
(I would think you would get javascript errors in the javascript console) This should work:
(我认为您会在 javascript 控制台中收到 javascript 错误)这应该有效:
allMarkers[j].setOptions({
icon: "http://labs.google.com/ridefinder/images/mm_20_white.png"
});