gpt4 book ai didi

javascript - 切换复选框以显示标记

转载 作者:行者123 更新时间:2023-11-28 06:09:17 24 4
gpt4 key购买 nike

I am trying to show the markers based on checkbox selection and unselection, but the problem i am facing is when the user continuously checks and unckecks the checkbox the marker will be multiplied and shown on maps. Can anyone help me resolving this.

我的 html 文件如下所示。

<input type="checkbox" style="float: right;" id="w" ng-click="getworkers()"></input>

我的用于显示标记和簇的 JavaScript 代码如下

$scope.getworkers = function(){         
clearOverlays();
bounds = new google.maps.LatLngBounds();
setTimeout(function(){
if(document.getElementById('w').checked){
for(var i=0;i<$scope.responseWorker.length;i++){

var latlng = new google.maps.LatLng($scope.responseWorker[i].rep_lat,$scope.responseWorker[i].rep_lon);
lat = $scope.responseWorker[i].rep_lat;
lng=$scope.responseWorker[i].rep_lon;
name=$scope.responseWorker[i].rep_name;
address=$scope.responseWorker[i].rep_address;

bounds.extend(latlng);
marker = new google.maps.Marker({
position: latlng,
map:map,
animation : google.maps.Animation.DROP,
icon: "lib/images/green-dot.png"
});
clusterobj.markers.push(marker);

//Info window
var infowindow = new google.maps.InfoWindow();
var content = "<table>" +"<head><h4>All Worker Details</h4></head>"+
"<tr><td>Name:</td> <td> "+name+" </td> </tr>" + "<tr><td>Address:</td> <td> "+address+"</td></tr>"+ "</table>";


google.maps.event.addListener(marker, 'click', (function(marker, content, infowindow) {
return function() {
if (infowindow)
infowindow.close();
infowindow.setContent(content);
infowindow.open(map, marker);
//map.setZoom(7);
};
})(marker, content, infowindow));

};
map.fitBounds(bounds);
var mcOptions = {styles: [{
gridSize: 40,
height: 53,
url: "http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/m1.png",
width: 53
},
]};
clusterobj.mc = new MarkerClusterer(map,clusterobj.markers,mcOptions);
}
else if(!document.getElementById('w').checked)
{
clusterobj.mc.clearMarkers();

}

},2000);

};

最佳答案

when the user continuously checks and unckecks the checkbox the marker will be multiplied and shown on maps

标记不断增加的原因是创建标记的代码是在 for 循环内生成的,并且每次选中复选框时都会触发。我建议你做的是:

  1. 创建一个使用 for 循环生成标记的函数。该函数仅运行一次。
  2. 创建单独的函数,在取消选中复选框时隐藏标记。另一个单独的函数,当勾选复选框时将显示标记。

演示代码为here .

关于javascript - 切换复选框以显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565268/

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