最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot remove markers in Google Maps JS API - Stack Overflow

programmeradmin1浏览0评论

I want to remove all markers on map when the map is clicked.

I am following the documentation here:

I've included the function:

function clearMarkers() {
  setMapOnAll(null);
}

to be triggered by a click event, but I just get the error:

Uncaught ReferenceError: setMapOnAll is not defined

There is no other information in the documentation that can help me.

Can anyone point me in the right direction?

I want to remove all markers on map when the map is clicked.

I am following the documentation here: https://developers.google./maps/documentation/javascript/examples/marker-remove

I've included the function:

function clearMarkers() {
  setMapOnAll(null);
}

to be triggered by a click event, but I just get the error:

Uncaught ReferenceError: setMapOnAll is not defined

There is no other information in the documentation that can help me.

Can anyone point me in the right direction?

Share Improve this question asked Nov 10, 2015 at 1:39 MeltingDogMeltingDog 15.6k52 gold badges178 silver badges322 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

The error is indicating that setMapOnAll does not exist in the scope

// Sets the map on all markers in the array.
function setMapOnAll(map) {
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(map);
  }
}

Responding to your last question, you do not really need to add the markers in an array, but be aware that in the documentation example, they add the word var before adding it in the array,

    var marker = new google.maps.Marker({
    position: location,
    map: map
    });
    markers.push(marker); ...

if you only want to work with just one marker then, do not add the var word

marker = new google.maps.Marker({...

Then use marker.setMap(null); to get it out off the map.

发布评论

评论列表(0)

  1. 暂无评论