2

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

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; 
        }   
    }
}
4

3 に答える 3

5

points.geometryはオブジェクトですが、配列のようにループしようとしています。座標にアクセスするには、次を使用します。

var coordinates = data.features[y].geometry.coordinates;
于 2011-09-12T15:25:33.490 に答える
1

私は geojson データを統合することを好みます =>map.data.addGeoJson(data);は、data単に機能の詳細を含む js 変数です。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();

};
于 2014-11-07T18:06:54.163 に答える
0

座標を取得するコードは次のとおりです

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

複数の配列がある可能性があります

var first = data.features[0].geometry.coordinates[0];
var second= data.features[0].geometry.coordinates[1];
于 2018-06-25T01:42:12.993 に答える