gpt4 book ai didi

javascript - 无法删除 Google Map v3 中的所有标记

转载 作者:行者123 更新时间:2023-11-30 08:48:41 25 4
gpt4 key购买 nike

我正在像这样添加制造商:

 var markers = [];
var map = null;
$.ajax({
type:"GET",
dataType:"json",
url:"<?php echo site_url("sandbox_faizan/get_coordinates") ?>",
success: function(result)
{
for(var i=0;i<result.length;i++)
{
var obj = [] ;
obj.lat=result[i].lat;
obj.lng=result[i].lng;
obj.count=result[i].Count;
// points.push(new google.maps.LatLng(result[i].lat,result[i].lng));
markers.push(obj);
//console.log(points[i]);
console.log(markers[i]);
}
addMarkers();
}
});
function addMarkers() {
// when the map is initialized and the points have been initialized, add them to the map
if ((map != null) && (markers.length > 0)) {
for (var i = 0; i < markers.length; i++) {
var marker = new MarkerWithLabel({
map: map,
position: new google.maps.LatLng(markers[i].lat,markers[i].lng),
icon: pinImage,
shadow: pinShadow,
labelContent: markers[i].count,
labelAnchor: new google.maps.Point(12, -5),
labelClass: "labels"
});
bounds.extend (new google.maps.LatLng(markers[i].lat,markers[i].lng));
map.fitBounds (bounds);
}
}
}

我正在清除这样的标记:

  function remove_markers(){

google.maps.Map.prototype.clearMarkers = function() {
for(var i=0; i < this.markers.length; i++){
this.markers[i].setMap(null);
alert("removing now"); // THIS DOES NOT ALERT
}
this.markers = new Array();
};
}

它什么都不做,我尝试了其他使用 setMap(null) 的方法,之前它给我错误 uncaught type error setMap not defined in console。

我做错了什么?

最佳答案

var marker = new MarkerWithLabel({
map: map,
position: new google.maps.LatLng(markers[i].lat,markers[i].lng),
icon: pinImage,
shadow: pinShadow,
labelContent: markers[i].count,
labelAnchor: new google.maps.Point(12, -5),
labelClass: "labels"
});
markersToRemove.push(marker); // markersToRemove is a global array

function removeMarkers() {
for(var i = 0; i < markersToRemove.length; i++) {
markersToRemove[i].setMap(null);
}
}

关于javascript - 无法删除 Google Map v3 中的所有标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19768590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com