2014-05-13 70 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(); 

感謝您的幫助。