Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 从地图上删除谷歌地图标记是可行的,现在不行了_Google Maps_Google Maps Api 3_Google Maps Markers - Fatal编程技术网

Google maps 从地图上删除谷歌地图标记是可行的,现在不行了

Google maps 从地图上删除谷歌地图标记是可行的,现在不行了,google-maps,google-maps-api-3,google-maps-markers,Google Maps,Google Maps Api 3,Google Maps Markers,当我在使用面向对象的javascript时,我创建了一个map对象,因为我需要维护所有以前的路由和标记,所以我没有创建新的map对象。我的代码如下 function map() { this.defaultMapOption = { center : new google.maps.LatLng(0, 0), zoom : 1, mapTypeId : google.maps.MapTypeId.ROADMAP };

当我在使用面向对象的javascript时,我创建了一个map对象,因为我需要维护所有以前的路由和标记,所以我没有创建新的map对象。我的代码如下

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);
                }
            });
        }
    });

}
只有上述代码仅适用于一个标记。表示双击时第一次删除标记。在那之后它就不起作用了


任何关于它为什么停止工作的想法

您正在添加标记[1],但随后尝试删除标记[0]。移动你的
count++从函数的开始到结束。

您的计数值随着新的标记而递增,在
addListener(map.marker[count],…)
中,count将包含最新的值。因此,只有该标记将被删除


因此,您应该在addListener函数的末尾递减
计数

没有。。我已经为它们中的每一个绑定了
双击
事件。这不是问题。Cdeez的答案与我说的完全相同。可能是,但我很容易理解@Cdeez的答案,然后是你的答案。。但你的答案意味着
标记
数组必须从0开始,我认为这不是问题。问题是bind函数。而@Cdeez明确表示,由于包含最新值的
计数将被删除..在Javascript中,每个数组都从索引0开始。是的,确实如此,但您显然对我的答案不感兴趣。很抱歉我试图帮助你ups。。你是对的。。我的愚蠢。。。需要以不同的方式完成。。非常感谢。:)