Google forEach map.data功能 - 返回功能LatLng [英] Google forEach map.data feature - return feature LatLng
问题描述
我正在尝试使用forEach遍历GeoJSON map.data。我想返回每个要素的位置(LatLng),以便根据要素属性将其添加到我的标记数组中。这是我的尝试:
allMarkers = [];
jQuery.getJSON('json.php',function(data){
points = map.data.addGeoJson(data);
});
var eid = 30;
$ b $ map.data.forEach(function(feature){
if(feature.getProperty('eid')=== eid){
LatLng = feature.getGeometry() .LatLng; //不知道如何获得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仅供参考。
任何提示/建议总是值得赞赏的。
我只需要修改feature.getGeometry()。latLng为feature.getGeometry()。get()就像@geocodezip在评论中提到的那样。
我尝试了feature.get(),这显然没有意义。不知道为什么我有疏忽。所以显然需要的是:
$ $ p $ LatLng = feature.getGeometry()。get();
感谢您的帮助。
I am trying to iterate through GeoJSON map.data using a forEach. I want to return the position (LatLng) of each feature so I can add it to my markers array based on a feature property. Here's my attempt:
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);
}
});
I want to create a marker for the ones I want and remove those from the map.data and keep the remaining map.data for reference.
Any tips/suggestions are always appreciated.
It turns out that all I needed to change was "feature.getGeometry().latLng" to "feature.getGeometry().get()" like what was mentioned in the comments by @geocodezip.
I had tried "feature.get()" and that obviously didn't make sense. Not sure why I had the oversight. So what was obviously needed was:
LatLng = feature.getGeometry().get();
Thanks for the help.
这篇关于Google forEach map.data功能 - 返回功能LatLng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!