admin管理员组文章数量:1399900
My code to make markers:
for (var marker in markers) {
var posMarker = new google.maps.Marker({
position: new google.maps.LatLng(markers[marker].lat, markers[marker].lng),
map: map,
visible: markers[marker].visible
});
};
My markers object:
var markers = {
"London": {"lat": -83.68088192646843, "lng": -125.270751953125, "type": "town", "visible": false},
"Paris": {"lat": -58.1548020417031, "lng": -21.318115234375, "type": "town", "visible": false},
};
I'm trying to be able to toggle the markers with a checkbox like so:
$('#toggle').change(function() {
for (var marker in markers) {
posMarker.setVisible(true);
};
});
But only the last marker in the array is shown, how do I make all of them appear?
Thanks.
My code to make markers:
for (var marker in markers) {
var posMarker = new google.maps.Marker({
position: new google.maps.LatLng(markers[marker].lat, markers[marker].lng),
map: map,
visible: markers[marker].visible
});
};
My markers object:
var markers = {
"London": {"lat": -83.68088192646843, "lng": -125.270751953125, "type": "town", "visible": false},
"Paris": {"lat": -58.1548020417031, "lng": -21.318115234375, "type": "town", "visible": false},
};
I'm trying to be able to toggle the markers with a checkbox like so:
$('#toggle').change(function() {
for (var marker in markers) {
posMarker.setVisible(true);
};
});
But only the last marker in the array is shown, how do I make all of them appear?
Thanks.
Share Improve this question asked Jun 29, 2012 at 23:49 HarryHarry 1,1501 gold badge12 silver badges16 bronze badges1 Answer
Reset to default 6Well, I see posMarker
being used as a temporary variable that places a Google Maps marker, and as the for loop progresses, the posMarker
reference "updates" to the latest marker placed. That's why only the last marker is being shown.
You need to keep track of all references to Google Maps markers being placed, including those that have been "consumed". My approach uses an object, much like your markers
object but holding references to Google Maps markers. You could also use a plain indexed array (posMarkers[]). It's up to you.
See the Demo, note the LatLngs have been modified for simplicity (looks like you have a custom coordinate system).
Also, I didn't make this change, but I just noticed that it may make more sense to call marker
in markers
, city
in markers
because the way your object is written. It would be more readable, but won't affect the execution.
Finally, semicolons at the end of for loops blocks are unneeded, and be careful with the trailing ma after the Paris
object (I'm guessing you just erased the rest of the list). In this case it didn't matter, but other times these trailing mas can be a source of hard-to-find bugs.
function initialize() {
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var markers = {
"London": { "lat": 0, "lng": 0, "type": "town", "visible": false },
"Paris": { "lat": 10, "lng": 10, "type": "town", "visible": false }
};
var posMarkers = {};
for (var marker in markers) {
posMarkers[marker] = new google.maps.Marker({
position: new google.maps.LatLng(markers[marker].lat, markers[marker].lng),
map: map,
visible: markers[marker].visible
});
}
$('#toggle').change(function () {
for (var marker in markers) {
if (posMarkers[marker].getVisible()) {
posMarkers[marker].setVisible(false);
}
else {
posMarkers[marker].setVisible(true);
}
}
});
}
本文标签: javascriptHow do I toggle markersStack Overflow
版权声明:本文标题:javascript - How do I toggle markers? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744186559a2594317.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论