使用javascript删除Google地图上的多个标记之一 [英] Remove one of many markers on Google maps with javascript

查看:88
本文介绍了使用javascript删除Google地图上的多个标记之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在使用下面的代码向地图添加标记。我希望能够通过推送JavaScript命令随时删除任何一个。这可能吗?



前。放置5个标记..移除第三个,同时保持其他4个。

  $('#map')。show() ; 
var geocoder = new google.maps.Geocoder();
var address = document.getElementById(address)。value +,+ document.getElementById(city)。value;

geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
var lat = results [0] .geometry.location.lat();
var lng = results [0] .geometry.location.lng();
locationsall [counter] = new Array();
locationsall [counter] [0] =地址;
locationsall [counter] [1] = lat;
locationsall [counter] [2] = lng;
var mapOptions = {
zoom:13,
center:new google.maps.LatLng(lat,lng),
mapTypeId:google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById(map_canvas),mapOptions);
var marker,i;
for(i = 0; i< locationsall.length; i ++ ){
marker = new google.maps.Marker({
position:new google.maps.LatLng(locationsall [i] [1],locationsall [i] [2]),
map:map,
title:'You are here!'
});
}
counter ++;
} else {
ale rt(地理编码不成功,原因如下:+状态);
}

});


编辑1:

$ $ p $ })。 text(Remove)。click(function(){
var index2 = $ tr2.index();
$(#locationDeals input [type ='hidden'])。each(function (){
this.name = this.name.replace(/ \ [(\ d +)\] /,function(str2,number2){
return[+(+ number2 > index2 - 1?number2 - 1:number2)+];
});
});
//locationsall[locations.indexOf(location)].setMap(null );
$ tr2.remove();
locations.splice(locations.indexOf(location),1);
return false;
})。appendTo($ tdRemoveRow2) ;


解决方案

您的 for循环之外定义一个 Array ,它将包含所有添加的标记。例如。 allMarkers = []



for循环中在创建标记之后,将其推入所述数组中。例如。 allMarkers.push(marker)



当您要移除第一个标记时: allMarkers [0] .setMap(null),或第三个标记: allMarkers [2] .setMap(null)


I currently add markers to the map using the code below. I'd like to be able to remove any one at any time by pushing a javascript command. Is this possible?

ex. place 5 markers.. removed the 3rd one while keeping the other 4.

$('#map').show();
var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value +", " + document.getElementById("city").value;

geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
    var lat = results[0].geometry.location.lat();
    var lng = results[0].geometry.location.lng();
    locationsall[counter] = new Array();
    locationsall[counter][0] = address;
    locationsall[counter][1] = lat;
    locationsall[counter][2] = lng;
    var mapOptions = {
        zoom: 13,
        center: new google.maps.LatLng(lat, lng),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker, i;
for (i = 0; i < locationsall.length; i++) {  
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locationsall[i][1], locationsall[i][2]),
    map: map,
    title: 'You are here!'
    });
}
counter++;
} else {
    alert("Geocode was not successful for the following reason: " + status);
}

}); }

Edit 1:

        }).text("Remove").click(function() {
    var index2 = $tr2.index();
    $("#locationDeals input[type='hidden']").each(function() {
        this.name = this.name.replace(/\[(\d+)\]/, function(str2, number2) {
          return "[" + (+number2 > index2 - 1 ? number2 - 1 : number2) + "]";
        });
    });
    //locationsall[locations.indexOf(location)].setMap(null);
    $tr2.remove();
    locations.splice(locations.indexOf(location), 1);
    return false;
        }).appendTo($tdRemoveRow2);

解决方案

Outside of your for loop define an Array that will hold all added markers. E.g. allMarkers = []

Inside of your for loop after creating the marker, push it in said Array. E.g. allMarkers.push(marker)

When you want to remove the first marker: allMarkers[0].setMap(null), or the third marker: allMarkers[2].setMap(null)

这篇关于使用javascript删除Google地图上的多个标记之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆