谷歌地图V3的setMap试图清除所有标记时未定义 [英] google maps v3 setMap undefined when trying to clear all markers

查看:159
本文介绍了谷歌地图V3的setMap试图清除所有标记时未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的人可以在这里解释一下这个功能:

I have seen may people here explain this function:

// REMOVE All MARKERS FUNCTION
    // Removes all markers currently on map
    // PARAMS: None
    function removeAllMarkers(){// removes all markers from map
        if (markersArray) {
            for (i in markersArray) {
                markersArray[i].setMap(null);
                markersArray = [];
                markersInfoArray = [];
            };
        };
    };

但我得到一个JavaScript错误...

but i get a javascript error...

歇上的错误
markersArray [I] .setMap不是一个函数

Break on Error markersArray[i].setMap is not a function

该网页是: http://www.focus-on-plants.com /locator.php

任何想法???

< ---------------更新--------------------->

<---------------Update--------------------->

我试过sugestions,也感动了 MarkersArray = [] markersInfoArray = [] 来的出方在fo​​r循环,所以我不得不这样的:

i tried the sugestions and also moved the MarkersArray=[] and markersInfoArray = [] to out side of the for loop so i had this:

for( var i = 0; i < markersArray.length; i++ ){
    markersArray[i].setMap(null);
}

但我得到同样的错误markersArray [I] .setMap不是一个函数

所以我环顾四周,尝试这种方式:

so i looked around and tried this method:

function removeAllMarkers(){// removes all markers from map
    alert('REMOVE MARKERS - markersArray count:'+ markersArray.length);
    while(markersArray[0]){
        markersArray.pop().setMap(null);
        markersInfoArray.pop()
    }
    markersArray.length = 0;
    markersInfoArray.length = 0;
};

和我仍然得到了同样的错误,怎么办?它几乎就像在的setMap()不存在,我在另一个线程<阅读href=\"http://stackoverflow.com/questions/1544739/google-maps-api-v3-how-to-remove-all-markers\">here它从set_map到的setMap(改变),但那些不为我工作之一:(

and i still get the same error, what gives? its almost as though the setMap() does not exist, i read in another thread here that it change from set_map to setMap() but those dont work for me either :(

推荐答案

确定,所以香港专业教育学院得到了它的工作,soloution是种丑陋,但它的工作原理,

ok so ive got it to work, the soloution is kind of ugly but it works,

我想通它不是从存储标记找到google.maps命名空间位在我的数组所以这就是为什么它认为存在这么想的功能。

i figured it wasnt finding the google.maps namespace bit from stored markers in my array so thats why it thought the function dosen't exist.

IE浏览器。它应该是 google.maps.marker.setMap()数组中的标志物只是输出纬度和经度。

ie. it should be google.maps.marker.setMap() the marker in the array was just outputting the lat and lng.

所以removeAllMarkers函数中我创建了一个新的google.maps.marker delmarker
从存储在标记中的数据来填充它的 markersArray
然后取出用这个新的参考地图的标记 delmarker.setMap(空)

so within the removeAllMarkers function i create a new google.maps.marker delmarker populate it with the data from the marker stored in the markersArray and then remove the marker of the map using this new reference delmarker.setMap(null)

和宾果它的工作原理,但冗长,带我一段时间才能弄清楚:)

and bingo it works, but longwinded and took me a while to figure out :)

function removeAllMarkers(){// removes all markers from map
    for( var i = 0; i < markersArray.length; i++ ){
    delmarker = new google.maps.Marker({
        position: markersArray[i]
    })
    delmarker.setMap(null)
    }   
    markersArray.length = 0;
    markersInfoArray.length = 0;
};

这篇关于谷歌地图V3的setMap试图清除所有标记时未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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