【发布时间】:2015-06-15 14:49:42
【问题描述】:
我正在尝试访问嵌套 JSON 文件中多边形的纬度/经度,并尝试使用 new google.maps.Polygon() 绘制它们,但遇到了一些麻烦。
UPDATE-2:我可以绘制多边形,但它只绘制第一个多边形,而不是全部 3:http://jsfiddle.net/vprbqaLm/10/
更新:在这里查看我最新的 JSFiddle:http://jsfiddle.net/vprbqaLm/9/
现在我似乎无法读取坐标,因为我收到以下错误:Uncaught TypeError: Cannot read property 'coordinates' of undefined
我的代码如下所示:
var coords = data.features.geometry.coordinates;
var paths = [];
for (i = 0; i < coords.length; i++) {
for (j = 0; j < coords[i].length; j++) {
var path = [];
for (k = 0; k < coords[i][j].length; k++) {
var ll = new google.maps.LatLng(coords[i][j][k][1], coords[i][j][k][0]);
path.push(ll);
}
paths.push(path);
}
}
var polygon = new google.maps.Polygon({
paths: paths,
strokeColor: "#FF7800",
strokeOpacity: 1,
strokeWeight: 5,
fillColor: "#46461F",
fillOpacity: 1
});
return polygon;
这是示例 JSON 文件:
var data = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"id": 1,
"properties": {
"Name": "",
"description": "",
"timestamp": "",
"begin": "",
"end": "",
"altitudeMode": "clampToGround",
"tessellate": 1,
"extrude": -1,
"visibility": -1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-83.126571,
42.348706],
[-83.126520,
42.348634],
[-83.126516,
42.348635],
[-83.126147,
42.348778],
[-83.126144,
42.348780],
[-83.126195,
42.348852],
[-83.126199,
42.348851],
[-83.126568,
42.348708],
[-83.126571,
42.348706]
]
]
}
}, {
"type": "Feature",
"id": 2,
"properties": {
"Name": "",
"description": "",
"timestamp": "",
"begin": "",
"end": "",
"altitudeMode": "clampToGround",
"tessellate": 1,
"extrude": -1,
"visibility": -1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-83.132805,
42.356496],
[-83.132753,
42.356423],
[-83.132751,
42.356424],
[-83.132243,
42.356624],
[-83.132241,
42.356625],
[-83.132294,
42.356698],
[-83.132296,
42.356697],
[-83.132802,
42.356497],
[-83.132805,
42.356496]
]
]
}
}, {
"type": "Feature",
"id": 3,
"properties": {
"Name": "",
"description": "",
"timestamp": "",
"begin": "",
"end": "",
"altitudeMode": "clampToGround",
"tessellate": 1,
"extrude": -1,
"visibility": -1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-83.126776,
42.351813],
[-83.126492,
42.351413],
[-83.126189,
42.351525],
[-83.126191,
42.351528],
[-83.126376,
42.351807],
[-83.126776,
42.351813]
]
]
}
}]
};
【问题讨论】:
-
我看到一些语法错误。你应该做一个小提琴(我在这里做了一个:jsfiddle.net/jetweedy/vprbqaLm)并在里面乱七八糟,这样我们就可以分享了。
-
试试
console.log(ai.latLngs)看看你会得到什么。 -
@JonathanTweedy 我添加了 Google Maps API,因此您现在可以在控制台上看到结果:jsfiddle.net/vprbqaLm/1
-
请提供一个Minimal, Complete, Tested and Readable example 来证明您的问题本身存在的问题。
-
@JonathanTweedy 什么语法错误?你能指出来吗?
标签: javascript jquery json google-maps