[英]Changing marker properties (google maps) when all marker-objects have the same name
I have a google map on my website that is populated with markers through PHP. 我的网站上有一张Google地图,其中通过PHP填充了标记。 What I don't understand is how I can identify each marker individually since all marker objects have the same name. 我不明白的是,由于所有标记对象都具有相同的名称,因此如何分别识别每个标记。
This code is taken from the google developers page ( https://developers.google.com/maps/articles/phpsqlajax_v3 ) 此代码取自Google开发人员页面( https://developers.google.com/maps/articles/phpsqlajax_v3 )
downloadUrl("phpsqlajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
For instance. 例如。 If I want to make one of the markers invisible I usually use marker.setVisible(false)
. 如果要使其中一个标记不可见,通常使用marker.setVisible(false)
。 However, in this case all markers have the same name ("marker"). 但是,在这种情况下,所有标记都具有相同的名称(“标记”)。
Any help greatly appreciated. 任何帮助,不胜感激。
You can keep track of all markers by keeping them in a list, for instance: 您可以通过将所有标记保存在列表中来跟踪所有标记,例如:
var markers = [];
for (/* .. */) {
/* .. */
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
markers.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
}
console.log(markers); // prints a list of all markers
You can then do: 然后,您可以执行以下操作:
markers[2].setVisible(false);
to make the marker at position 2 invisible. 使位置2处的标记不可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.