2014-05-13 89 views
1

我想通过使用forEach遍历GeoJSON map.data。我想返回每个要素的位置(LatLng),以便根据要素属性将其添加到我的标记数组中。这里是我的尝试:Google forEach map.data功能 - 返回功能LatLng

allMarkers = []; 
jQuery.getJSON('json.php', function(data){ 
     points = map.data.addGeoJson(data); 
}); 
var eid = 30; 

map.data.forEach(function(feature){ 
    if(feature.getProperty('eid') === eid){ 
     LatLng = feature.getGeometry().LatLng; //not sure how to get LatLng 
     id = feature.getProerty('id'); 
     var marker = new google.maps.Marker({ 
      position: LatLng, 
      map: map, 
      draggable: true, 
      id: id, 
      icon: imageActive, 
     }); 
     allMarkers[id] = marker; 
     map.data.remove(feature); 
    } 
    }); 

我想创造我想要的那些标记,并从map.data删除这些并保留剩余map.data参考。

任何提示/建议总是赞赏。

+0

在文档中描述的[get()](https://developers.google.com/maps/documentation/javascript/3.exp/reference#Data.Geometry)函数不适合您吗? – geocodezip

+0

请提供一个演示get()的行为的小提琴。 – geocodezip

回答

2

事实证明,我需要改变的是“feature.getGeometry()。latLng”到“feature.getGeometry()。get()”,就像@geocodezip在评论中提到的一样。

我曾试过“feature.get()”,这显然没有意义。不知道为什么我有疏忽。所以明显需要的是:

LatLng = feature.getGeometry().get(); 

感谢您的帮助。