Jquery 删除谷歌地图上的标记

Jquery 删除谷歌地图上的标记,jquery,google-maps,google-maps-api-3,Jquery,Google Maps,Google Maps Api 3,我对用ajax(with.each)填充标记的google地图有一个问题 我不使用任何数组 function parks() { $.ajax({ type: 'POST', dataType: 'json', url: '/index.php/route/getParks', success: function(data) { $.each(data

我对用ajax(with.each)填充标记的google地图有一个问题 我不使用任何数组

function parks() {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/index.php/route/getParks', 
            success: function(data) {
                $.each(data.results, function(k,e){
                    var myLatlng = new google.maps.LatLng(e.lat,e.lng);
                    var marker = new google.maps.Marker({
                        position: myLatlng,
                        map: mapg,
                        title: 'Places',
                    });
                })
            }
        });
    }
是否有解决方案,或者我必须使用数组来执行此操作


谢谢

您不希望它没有阵列。您需要搜索整个
mapg
对象,找到相应的标记区域,找到您的标记和bla-bla-bla;非常痛苦。尤其是单字母变量名

而是定义一个全局标记数组:
markers=newarray()

添加标记时填充数组:

$.each(data.results, function(k,e){
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(e.lat,e.lng),
        map: mapg,
        title: 'Places',
        id: k // you may want to give another value for easy recognition
    });

    // you may add like markers["myPlace"] = marker; for easy recognition
    markers.push(marker); 
});
然后,您可以通过查看
标记
数组获得标记,并从地图中删除:

markers["myPlace"].setMap(null);
// or you can search by id by looking markers[index].id in a loop

您需要一个标记数组,遍历数组并调用
setMap(null)在每个标记上,除非重新加载地图是一个选项。