0
我有多個標記的地圖,我已經幾乎實現了一種οf在地圖上的過濾器。當我點擊標記時,標記消失,並且一個按鈕顯示在地圖的頂部以恢復標記 - 這是不起作用的。我試圖在用戶點擊按鈕時重新創建標記,但似乎不起作用。有任何想法嗎?這裏是我的代碼:恢復對谷歌刪除標記映射
function getCrimeByLocation(lat, lng, date){
if(date == null){
var d = new Date();
date = d.getFullYear() + '-' + (d.getMonth()+1);
//hardcoding as of now as jan 2014 data is not there, remove when req
date = "2013-02";
}
$.getJSON("http://data.police.uk/api/crimes-street/all-crime?lat="+lat+"&lng="+lng+"&date="+date, function(data) {
while(markers.length > 0){
markers.pop().setMap(null);
}
//marking the requested position
addMyPositionMarker(lat, lng);
//loop through the data
$.each(data, function(key, val) {
//var myLatlng = new google.maps.LatLng(val.location.latitude, val.location.longitude);
//create the markers on the map
var marker = new google.maps.Marker({
position: new google.maps.LatLng(val.location.latitude, val.location.longitude),
map: map,
animation: google.maps.Animation.DROP,
draggable: false,
title: val.location.street.name
});
markers.push(marker); // saving markers for reference, so that we can remove them later;
//'click' event to delete the markers from the map
google.maps.event.addListener(marker, 'click', function(){
for(var i = 0; i < markers.length; i++){
markers[i].setMap(null);
}
markers.length = 0;
//Once the markers are deleted, the button shows up
antisocial.show();
});
});
var antisocial = $('<button>Anti-social behaviour</button>').hide().click(function(){
//Here I tried to restore the markers, but it doesn't work
var marker = new google.maps.Marker({
position: new google.maps.LatLng(val.location.latitude, val.location.longitude),
map: map,
animation: google.maps.Animation.DROP,
draggable: false,
title: val.location.street.name
});
markers.push(marker);
//hide the button once it's clicked
antisocial.hide();
});
$('#map-canvas').before(antisocial);
if(markers.length > 0){
fitBoundsMap();
}
});
}
感謝。
感謝您的回答。當我在標記上創建'click'事件並且工作正常時,我將地圖設置爲null。此按鈕也顯示在最上面。爲了再次顯示它們,我嘗試重新創建標記以及'setMap(markers)',但其中沒有一個正在工作。 –
沒有必要重建他們,他們應該仍然存在,只是設置了地圖回是怎麼回事。 –
仍然不明白。你的意思是用'markers.push(marker)'? –