【问题标题】:Issue on Removing Markers From Map从地图中删除标记的问题
【发布时间】:2014-01-01 18:55:50
【问题描述】:

您能否查看this Link 并告诉我为什么我无法从地图中删除标记?我收到此错误消息:

Uncaught TypeError: Object [object Array] has no method 'setMap'

代码:

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
$( "#trg" ).click(function() {
 $.ajax({
    // type: "POST",
    url: 'SO_20131215_data.json.txt',
    dataType: 'json',

    success: function(data){ 
      locations.length = 0;
      for (p = 0; p < data.markers.length; p++) {
        locations.push(Array(data.markers[p].latitude,data.markers[p].longitude));
        // To add the marker to the map, use the 'map' property
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(data.markers[p].latitude,data.markers[p].longitude),
          map: map,
          title:"marker "+p
        });
      }
    },
    error: function (xhr,status,errorStr) {
      alert("status="+status+", error="+errorStr);
    }

  }); 

});
  function removeMarkers() {
            for (i = 0; i < locations.length; i++) {
                locations[i].setMap(null)
            }
        }

  $('#remover').click(function () {
            removeMarkers();
        });

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:
     function removeMarkers() {
            for (i = 0; i < locations.length; i++) {
                if(locations[i])
                     locations[i].setMap(null)
            }
        }
    

    还有一件事,在 ajax 成功函数内部:

       for (p = 0; p < data.markers.length; p++) {
    
          //locations.push(Array(data.markers[p].latitude,data.markers[p].longitude));
    
          // To add the marker to the map, use the 'map' property
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(data.markers[p].latitude,data.markers[p].longitude),
          map: map,
          title:"marker "+p
        });
    
        locations.push(marker);
    
    
      }
    

    【讨论】:

      【解决方案2】:

      在调试您的代码时,似乎 您指的是地图的位置(纬度/经度) 就像

      locations[i].setMap(null)  //WRONG
      

      这是错误的。正确使用setMap 是用谷歌地图的标记之类的

      function removeMarkers() {
                  for (i = 0; i < marker.length; i++) {
                      marker[i].setMap(null);  //refer marker
                      marker[i] = null;  //if you wish to delete the marker
                  }
              }
      

      仅供参考:在您的代码中,marker 变量是本地范围的,因此请尝试将其更改为全局范围。

      【讨论】:

      • 感谢 Praveen,但现在我收到此错误:未捕获的 ReferenceError:未定义标记
      • 您能告诉我如何访问本地范围的变量吗?
      • @Behseini 只需从 var marker = new google.maps.Marker({ position: new google.maps.LatLng(data.markers[p].latitude,data.markers[p].longitude), map: map, title:"marker "+p }); 中删除 var 或在 map 变量旁边声明 var marker; 以便全局访问
      • @Behseini 抱歉,在全局范围内尝试像 var markers = []; 一样,然后在标记之后将每个标记推送到此数组,如 markers.push(marker);。锄头你能理解。
      • 感谢 Praveen,我试过但没用,但 Asif 解决方案有效。非常感谢您的 cmets
      猜你喜欢
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2016-01-13
      • 1970-01-01
      • 2013-04-10
      • 2015-04-09
      相关资源
      最近更新 更多