在Google Maps API V3中启用和禁用大量标记 [英] Enabling and Disabling large amounts of markers in Google Maps API V3
问题描述
我有一个程序,上面有两千个标记.性能不是这里的问题,但组织标记以使它们有意义才是我要追求的目标.我当时正在考虑制作几个复选框,例如,打开和关闭具有500个标记的第1组,而使第2个具有200个标记的组离开.但是,在我的研究中,我还没有找到一种可行的方法为此.
I have a program that will have a couple thousand markers on it. Performance isn't the issue here, but organizing the markers so they make sense is what I am going for. I was thinking about making a few check boxes and for example, turning group 1 which has 500 markers on and off and leave group 2 on which has say 200. I have not, however, in my research been able to figure out a viable method for this.
我找到了以下示例: http://www.geocodezip.com/v2_MarkerCheckBoxesNoXml.asp
但是,如果您看一下它的代码,则将按以下方式创建标记:
However, if you look at the code for it markers are created like this:
var point = new GLatLng(43.545068,-89.994888);
var marker = createMarker(point, '<div id="infowindow" style="white-space: nowrap;">
church of god<br>1225 n dewey ave<br>Reedsburg, WI<br><a
href=http://> </a></div>','purple');
单个标记有很多代码.
我希望它是这样的:
var yellowMarkers = new [[39.9406864, -77.8082025],
[33.4482117, -112.0709371],[42.922825, -85.6523916],[40.331837, -79.3783739]];
for (var i = 0; i < yellowMarkers.length; i++) {
var place = locations[i];
var myLatLng = new google.maps.LatLng(place[1], place[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
// icon: image,
shape: shape,
title: place[0],
zIndex: place[3]
});}
我是Java语言的新手,所以如果这是一个愚蠢的问题,请给我发送正确的信息...但是,有没有一种方法可以像在该示例中那样使用数组集来组织标记,而不是单独创建每个标记?
I am new to javascript so if this is a dumb question just send me the right way... but is there a way to organize markers like you would from that example using sets of arrays instead of creating every marker individually?
推荐答案
您需要为每个标记组创建一个数组,然后将每个标记推入正确的数组.完成此操作后,您可以轻松切换每组标记的可见性(或从地图中添加/删除).
You need to create an array for each markers group and push each marker to the correct array. Once this is done, you can easily toggle the visibility (or add/remove from the map) for each group of markers.
例如,如果我将您的代码与yellowMarkers
一起使用:
For example, if I take your code with yellowMarkers
:
var yellowMarkers = [[39.9406864, -77.8082025], [33.4482117, -112.0709371], [42.922825, -85.6523916], [40.331837, -79.3783739]];
// Create an empty array to hold your yellow markers
var yellowMarkersArray = [];
for (var i = 0; i < yellowMarkers.length; i++) {
var place = locations[i];
var myLatLng = new google.maps.LatLng(place[1], place[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
shape: shape,
title: place[0],
zIndex: place[3]
});
// Push each yellow marker to the corresponding array
yellowMarkersArray.push(marker);
}
然后在另一个功能中隐藏您的黄色标记:
Then in another function to hide your yellow markers:
for (var i=0; i<yellowMarkersArray.length; i++) {
yellowMarkersArray[i].setVisible(false);
}
,或者如果您要将其从地图中删除:
or if you want to remove them from the map:
for (var i=0; i<yellowMarkersArray.length; i++) {
yellowMarkersArray[i].setMap(null);
}
,当然,如果要添加回去,请使用setVisible(true)
(如果您使用的是第一种方法),或者使用setMap(map)
(map是您的地图对象).
and of course if you want to add them back, either use setVisible(true)
if you used the first method, or setMap(map)
(map being your map object).
for (var i=0; i<yellowMarkersArray.length; i++) {
yellowMarkersArray[i].setMap(map);
}
希望这会有所帮助!
这篇关于在Google Maps API V3中启用和禁用大量标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!