【问题标题】:Google Maps API remove marker by latitude/longitudeGoogle Maps API 按纬度/经度删除标记
【发布时间】:2013-03-09 00:45:46
【问题描述】:

我有一个不断更新标记的谷歌地图。如果标记重叠(完全相同的位置),我有一个计数器可以添加到标记内的数字。问题是每次我添加一个新标记时,它并不总是把它放在另一个标记的顶部。因此,例如,标记中有数字 10。一个新的标记随着 11 下降,在动画之后它隐藏在 10 后面。有时在几个标记之后,顶部标记将是正确的数字,例如 21 而不是 10。

那么有没有办法在添加新标记之前按纬度/经度删除标记?这是我所拥有的:

for (var i=0; i<response.trans.length; i++) {
    //add to location 
    location[response.trans[i].location_id] += 1;

    latLng = new google.maps.LatLng(response.trans[i].latitude, response.trans[i].longitude);
    marker = new google.maps.Marker({
         map:map,                                                           
         position: latLng, 
         icon: 'http://chart.apis.google.com/chart?chst=d_map_spin&chld=1|0|FF0000|12|_|'+client[response.trans[i].location_id], 
         animation: google.maps.Animation.DROP
   });
   markers.push(marker);
}

谢谢雷蒙德!你把我推向了正确的方向

这是对我有用的代码:

for (var i=0; i<response.trans.length; i++) {
    //add to total count
    totalCount += 1;

    //add to location
    location[response.trans[i].location_id] += 1;

    markerChanged = false;
    for(x=0; x < markers.length; x++){
        if(markers[x].getPosition().lat().toFixed(6) == response.trans[i].latitude.toFixed(6) && markers[x].getPosition().lng().toFixed(6) == response.trans[i].longitude.toFixed(6)){
            markers[x].setIcon('http://chart.apis.google.com/chart?chst=d_map_spin&chld=1|0|FF0000|12|_|'+location[response.trans[i].location_id]);
            markers[x].setAnimation(google.maps.Animation.DROP);
            markerChanged = true;
        }
    }

    if(markerChanged === false){
        latLng = new google.maps.LatLng(response.trans[i].latitude, response.trans[i].longitude);
        marker = new google.maps.Marker({
            map:map,
            position: latLng, 
            icon: 'http://chart.apis.google.com/chart?chst=d_map_spin&chld=1|0|FF0000|12|_|'+location[response.trans[i].location_id], 
            animation: google.maps.Animation.DROP
        });
        markers.push(marker);
     }
 }

所以我添加了内部 for 循环,它检查当前标记。我不得不使用 toFixed(6) 将其更改为字符串。浮点精度正在抛弃准确性。因此,如果标记相同,它将更改图标。如果它是一个新标记,它会将其添加到地图中。我添加了 DROP 动画,因为我仍然希望它明显更新。

谢谢!

【问题讨论】:

    标签: javascript google-maps marker


    【解决方案1】:

    我相信您可以在将标记添加到地图之前汇总列表的信息。也就是说,

    var location = {};
    
    for (var i = 0; i < response.trans.length; i++) {
      location[response.trans[i].location_id] += 1;
    }
    
    for (location_id in location) {
      if (location.hasOwnProperty(location_id)) {
        /* add marker */
      }
    }
    

    【讨论】:

      【解决方案2】:

      如果它们的确切位置相同,您可以尝试:

      for(i=0; i < markers.length; i++) {
          if(markers[i].getPosition().lat() == response.trans[i].latitude && markers[i].getPosition().lng() == response.trans[i].longitude)
              markers[i].icon = "number11.png"
      }
      

      【讨论】:

      • Do NOT 使用未记录的属性 (.ib .jb),它们更改并意外破坏您的代码 [使用记录的方式访问它们:.lat() 和 .lng()]。此外,浮点相等也不是一个好的测试。
      • 谢谢,我正在一个项目中使用它,这对我有帮助。
      • 这将我推向了正确的方向!谢谢!我将使用更新的代码更新我的问题。
      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多