2013-03-09 72 views
1

我有一个谷歌地图,我不断用标记更新。如果标记重叠(完全相同的位置),我有一个计数器添加到标记内的数字。问题是每次我添加一个新标记时,并不总是将它放在另一个标记的顶部。所以,例如,标记的数字是10。一个新的标记与11和动画后它隐藏在10后面。有些时候在几个标记后,顶部标记将是正确的数字,如21而不是10.Google Maps API按经度/纬度移除标记

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

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动画,因为我仍然希望它能够被更新。

谢谢!

回答

0

如果他们是相同的确切位置,你可以尝试:

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" 
} 
+2

待办事项_使用无证属性(.IB .jb),他们_will_改变,会意外地破坏你的代码[使用以文档方式访问它们:.lat()和.lng()]。而且,浮点平等不是一个好的测试。 – geocodezip 2013-03-09 01:44:07

+0

谢谢,我目前正在使用这个项目,这将帮助我。 – Ray 2013-03-09 02:47:58

+0

这让我朝着正确的方向前进!谢谢!我会用更新的代码更新我的问题。 – Pablo 2013-03-11 15:53:13

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 */ 
    } 
} 
相关问题