【发布时间】:2016-07-06 00:50:06
【问题描述】:
我有一个 geoJSON 文件,其中包含标记的 ID 以及每个标记的纬度和经度。 (见下面的 JSON)。
我正在使用$getJSON 来访问它的属性。
geoJSON 文件不断变化(添加或删除标记)。所以,我要做的是遍历每个标记并创建我的文件具有的变量数量,然后我将为每个变量设置 Click 事件侦听器。
我想要以下内容:
$.getJSON("MyFile.geojson",
function(JSON_Result) {
var JSON_Features_Count = JSON_Result['features'].length;
for (i=0; i<JSON_Features_Count; i++){
var coordinates = JSON_Result['features'][i]['geometry']['coordinates']; //Lat = coordinates[1]; Long = coordinates[0]
var Marker_i = // this variable should be dynamic, because I don't know the number of markers my file has.
new google.maps.Marker({
position: new google.maps.LatLng(coordinates[1],coordinates[0]),
map: map,
});
}
});
这是我的 JSON 文件的示例:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "ID": 1}, "geometry": { "type": "Point", "coordinates": [ -43.256001299999895, -18.966999944999898] } },
{ "type": "Feature", "properties": { "ID": 2}, "geometry": { "type": "Point", "coordinates": [ -43.3659564559999, -18.733241581 ] } },
]
}
【问题讨论】:
标签: javascript jquery json google-maps