简体   繁体   English

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

[英]Accessing Coordinates in a nested geojson file to draw polygons with Google Maps API v3

I am having a real pain of trying to draw multiple polygons from a geojson file. 尝试从geojson文件绘制多个多边形让我非常痛苦。 Below I have pasted a sample of the geojson file and the javascript used to access it. 下面,我粘贴了geojson文件的示例以及用于访问它的javascript。 It seems the main problem I am running into is that I cannot get into that array of coordinates nested in each record as it either returns the error that "coordinates" is undefined or that there is no method "setMap" for undefined. 似乎我遇到的主要问题是我无法进入嵌套在每个记录中的坐标数组,因为它返回的错误是“ coordinates”未定义,或者没有方法“ setMap”用于未定义。 I have been able to return other nested aspects of a similar JSON file (this is a test file, the real one actually has data, just trying to get the polygon drawing here), but getting those coordinates is not working. 我已经能够返回类似JSON文件的其他嵌套方面(这是一个测试文件,实际的文件实际上具有数据,只是试图在此处获取多边形图),但是获取这些坐标是行不通的。 I am not a javascript master so I can't tell where the code is failing to make the proper access. 我不是javascript大师,所以我无法确定代码在哪里无法正确访问。

thanks in advance. 提前致谢。

the json data looks like this: 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...
    ]
}

I've got the javascript as below, using the example used at geojason.info: http://demos.geojason.info/complex-geojson-polygons-google-maps-demo.php 我使用geojason.info中使用的示例获取了以下javascript: 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 is an object, but you're trying to loop through it like an array. points.geometry是一个对象,但是您试图像数组一样遍历它。 To access the coordinates, use: 要访问坐标,请使用:

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

I'm prefering to integrate the geojson data => map.data.addGeoJson(data); 我更喜欢整合geojson数据=> map.data.addGeoJson(data); where data is simply your js var containing the features details. 其中data只是包含功能详细信息的js var。 here's an example of initialize() function : 这是initialize()函数的示例:

function initialize() {


   // Create a simple map.
    map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 11,
        center: {
            lat: 42.347727,
            lng: -83.058014
        },
        mapTypeId: google.maps.MapTypeId.TERRAIN
    });

    // Load a GeoJSON from the same server as our demo.
    map.data.addGeoJson(mapdata);

    setStyle();

};

Here is the code to get coordinate 这是获取坐标的代码

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

There maybe more than one arrays 可能不止一个阵列

var first = data.features[0].geometry.coordinates[0];
var second= data.features[0].geometry.coordinates[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM