javascript - 访问嵌套 geojson 文件中的坐标以使用 Google Maps API v3 绘制多边形

标签 javascript google-maps-api-3 polygons geojson

尝试从 geojson 文件中绘制多个多边形时,我真的很痛苦。下面我粘贴了 geojson 文件的示例以及用于访问它的 javascript。看来我遇到的主要问题是我无法进入每个记录中嵌套的坐标数组,因为它要么返回“坐标”未定义的错误,要么没有未定义的方法“setMap”。我已经能够返回类似 JSON 文件的其他嵌套方面(这是一个测试文件,真实的文件实际上有数据,只是试图在此处获取多边形绘图),但获取这些坐标不起作用。我不是 javascript 大师,所以我无法判断代码在哪里无法进行正确的访问。

提前致谢。

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
                        ]
                    ]
                ]
            }
        }
    etc...
    ]
}

我得到了如下的 javascript,使用 geojason.info 中使用的示例:http://demos.geojason.info/complex-geojson-polygons-google-maps-demo.php

var points;
var pointsMore;
var polygon;
var map;


function initializeMap() {

    map = new google.maps.Map(document.getElementById("map_canvas"), {
        zoom: 11,
        center: new google.maps.LatLng(42.347727, -83.058014),
        mapTypeId: google.maps.MapTypeId.TERRAIN
    });

    var polygon = createPolygons(pointsMore);
    //this is where the problem is...check nesting.
    polygon.setMap(map);

}


function createPolygons(pointsMore) {
    for (var y = 0; y < data.features.length; y++) {
        var points = data.features[y];
        for (var z = 0; points.geometry.length; z++) {
            var pointsMore = points.geometry[z];



    var coords = pointsMore.coordinates;
    var paths = [];
    $.each(coords,function(i,n){
        $.each(n, function(j,o){
           var path = [];
           $.each(o,function(k,p){
               var ll = new google.maps.LatLng(p[1],[0]);
               path.push(ll);
           });
           paths.push(path); 
        });
    });
    var polygon = new google.maps.Polygon({
        paths: paths,
        strokeColor: "#FF7800",
        strokeOpacity: 1,
        strokeWeight: 2,
        fillColor: "#46461F",
        fillOpacity: 0.25
    });
    return polygon; 
        }   
    }
}

最佳答案

points.geometry 是一个对象,但您试图像数组一样循环遍历它。要访问坐标,请使用:

var coordinates = data.features[y].geometry.coordinates;

关于javascript - 访问嵌套 geojson 文件中的坐标以使用 Google Maps API v3 绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7389811/

相关文章:

javascript - 谷歌地图 API : Want geolocation to click automatically on data layer below

outline - 给定非凸多边形中的大量顶点,我如何找到边?

javascript - 分组多边形

python - 连续排列线段组成多边形

php - 通过Ajax调用PHP文件中的MySQL连接单例对象

javascript - 对 JavaScript 数组的内容求和

javascript - Google Maps API v3 点击事件未通过自定义叠加层传递

Javascript:如何以毫秒计算一天的开始?

javascript - 原生 Android 浏览器上的链接问题

javascript - 如何从数据库查询中取出 JavaScript 对象以在脚本的其余部分中进行访问