由于我正在处理面向对象的JavaScript,因此我创建了一个地图对象,并且因为我需要保留以前的所有路线和标记,所以我不会创建新的Map对象。我的代码如下从地图中删除Google地图标记正在工作,现在它不是
function map() {
this.defaultMapOption = {
center : new google.maps.LatLng(0, 0),
zoom : 1,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
this.map = null;
this.marker = [];
this.directionsDisplay = null;
}
map.prototype.init = function() {
this.map = new google.maps.Map(document.getElementById("map"), this.defaultMapOption);
var map1 = this.map;
var marker1 = this.marker;
var count = 0;
google.maps.event.addListener(this.map, 'click', function(event) {
count++;
$(".tabNavigation li").find("a[href=markers]").trigger('click');
if (marker1[count]) {
marker1[count].setPosition(event.latLng);
}
else
{
marker1[count] = new google.maps.Marker({
position: event.latLng,
map: map1,
draggable : true
});
//by clicking double click on marker, marker must be removed.
google.maps.event.addListener(map.marker[count], "dblclick", function() {
console.log(map.marker);
map.marker[count].setMap(null);//this was working previously
map.marker[count].setVisible(false);//added this today
$("#markers ul li[rel='"+count+"']").remove();
});
google.maps.event.addListener(marker1[count], "dragend", function(innerEvent) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': innerEvent.latLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.addMarkerData(results, count);
}
else
{
alert("Geocoder failed due to: " + status);
}
});
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': event.latLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.addMarkerData(results, count);
}
else
{
alert("Geocoder failed due to: " + status);
}
});
}
});
}
只有上面的代码只适用于一个标记。意味着第一次标记被双击删除。之后,它不会工作。
任何想法,为什么它停止工作!
ups ..你是对的..我的愚蠢......需要以不同的方式完成......非常感谢。 :) – KuKu