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
Javascript 函数无法从Google地图中删除标记_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 函数无法从Google地图中删除标记

Javascript 函数无法从Google地图中删除标记,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,调用下面的函数时不会删除标记。为什么? // Remove existing markers from the Map function removeMarkers(markersArray) { var j,position; for (j = 0; j < markersArray.length; j += 1){ position = new google.maps.Marker({ position: {lat: marke

调用下面的函数时不会删除标记。为什么?

// Remove existing markers from the Map
function removeMarkers(markersArray) {
    var j,position;

    for (j = 0; j < markersArray.length; j += 1){
        position = new google.maps.Marker({
            position: {lat: markersArray[j][1], lng: markersArray[j][2]}
        });

        position.setMap(null);
    };
};

如果循环中的标记是正确的,只需使用此更改for循环,如果可能,制作一个小提琴手

  for (j = 0; j < markersArray.length; j += 1){
    position[j] = new google.maps.Marker({
        position: {lat: markersArray[j][1], lng: markersArray[j][2]}
    });

    position[j].setMap(null);
  };
(j=0;j{ 位置[j]=新的google.maps.Marker({ 位置:{lat:markersArray[j][1],lng:markersArray[j][2]} }); 位置[j]。设置映射(空); };
您需要保留所放置标记的参考

var placedMarkers = [];

function placeMarkers(markersArray) {
    var marker, i, postiion, bounds = new google.maps.LatLngBounds();
    for (i = 0; i < markersArray.length; i += 1) {
        marker = new google.maps.Marker({
            map: map,
            position: {
                lat: markersArray[i][1],
                lng: markersArray[i][2]
            }
        });

        // keep reference of the markers you placed
        placedMarkers.push(marker);

        position = new google.maps.LatLng(markersArray[i][1], markersArray[i][2]);
        bounds.extend(position);
    }
    map.fitBounds(bounds);
};
var placedMarkers=[];
函数placeMarkers(markersArray){
var marker,i,position,bounds=new google.maps.LatLngBounds();
对于(i=0;i
移除标记时,请使用保留的引用数组

// Remove existing markers from the Map
function removeMarkers() {
    var j,position;

    // loop through reference array you kept.
    for (j = 0; j < placedMarkers.length; j += 1){
        placedMarkers[j].setMap(null);
    };
};
//从地图中删除现有标记
函数removeMarkers(){
变量j,位置;
//循环遍历您保留的引用数组。
对于(j=0;j
您能否演示如何放置标记?您必须删除现有标记。您的代码正在创建新的标记,而不是将它们添加到地图中……下面是我用来创建标记的函数。一旦这些标记出现在地图上,我希望能够将它们全部删除<代码>//在地图上放置新标记//函数placeMarkers(markersArray){var marker,i,position,bounds=New google.maps.LatLngBounds();for(i=0;i
markersArray
是字符串数组的数组。因此,
setMap
不是函数感谢@never编辑代码,不确定OP创建的标记是否正确。我还必须在for循环后的removeMarkers()函数中添加以下代码行:
placedMarkers=[];
// Remove existing markers from the Map
function removeMarkers() {
    var j,position;

    // loop through reference array you kept.
    for (j = 0; j < placedMarkers.length; j += 1){
        placedMarkers[j].setMap(null);
    };
};