【问题标题】:Accessing Coordinates in Nested JSON File访问嵌套 JSON 文件中的坐标
【发布时间】: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


【解决方案1】:

两个建议

first:: 你可以在没有

的情况下分配坐标
  $.each(polygons, function (i, n) {
   .....
  }

你已经有了 javascrip 格式的坐标[[......],[.....]],那么你不需要重新分配给paths[]

第二个似乎你有一个嵌套的 [] 对你的格式进行了很多尝试,然后是一个负嵌套级别。

JSON 坐标文字示例:

[[{"lat": 41.650299987709538, "lng": 12.536399034779624}, {"lat": 41.650331001957937, "lng": 12.53657301029202}, {"lat": 41.650344029686487, "lng": 12.537146999950506}, {"lat": 41.650474995651187 , "lng": 12.537842047638419}, {"lat": 41.650461970948285, "lng": 12.538146026115472}, {"lat": 41.65040398326272, "lng": 12.538020010393916}, {"lat": 41.650271995260688, "lng": 12.537231036701897}, {"lat": 41.650243023870289, "lng": 12.536492993378628}, {"lat": 41.650299987709538, "lng": 12.536399034779624}]]

如果您可以按照示例中的格式进行格式化,则可以进行直接分配,否则您必须转换坐标中的每对数字,将它们添加到数组中,然后将其分配给路径。

【讨论】:

  • 我不确定你的意思。你能在这里看到 JSFiddle 吗? jsfiddle.net/vprbqaLm/1 我认为他们需要作为new google.maps.LatLng() 传递。
  • 我的意思是:paths = polygons; 。对于每个特征,JSON 中的坐标是路径
  • 好的。我尝试在路径中添加data.features[i].geometry.coordinates,但出现错误? InvalidValueError: at index 0: at index 0: not a LatLng or LatLngLiteral: in property lat: not a number
  • 对不起,我没有注意到您只使用坐标的数字而不使用对象文字坐标的事实。我编辑了答案,给出了包含 JSON 坐标的轨道示例,可以直接使用,就像我之前提到的那样
  • 看看这个jsfiddle.net/pc7tux02/1这个工作正常。在您的示例中,您没有在 for cicle 内绘制多边形,然后只绘制最后一个,第二个路径变量和多边形变量被声明在错误的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 1970-01-01
  • 2019-12-05
  • 2019-11-30
相关资源
最近更新 更多