1

別の GraphHopper レイヤー内に表示できるようにしたい POI を含む GeoJSON ファイルがあります。何度か試してインターネットで検索した後、どうにかしてそれを行う方法を見つけることができません。

これは GeoJSON ファイルのサンプルです (JSON バリデーターでファイル全体をチェックしたところ、問題ありませんでした)。

{"type": "Feature", "properties": { "fee": "no", "bicycle_parking": "anchors", "ref": "PVNAN23", "address": "Rue Gabriel Goudy 44200 Nantes", "name": "Pirmil P+R", "capacity": "24", "park_ride": "yes", "amenity": "bicycle_parking", "covered": "yes" }, "geometry": {" type": "Point", "coordinates": [-1.5406709, 47.1960031]}}, {"type": "Feature", "properties": { "bicycle_parking": "stands", "addr:postcode": "44000" ", "addr:country": "FR","name": "Madeleine", "capacity": "6", "amenity": "bicycle_parking", "addr:street": "chaussée de la Madeleine", "note": "verifié", "addr:city" : "Nantes", "covered": "no", "addr:housenumber": "35" }, "geometry": {"type": "Point", "coordinates": [-1.55076671448, 47.21000114109]}}対象": "いいえ", "addr:housenumber": "35" }, "geometry": {"type": "Point", "coordinates": [-1.55076671448, 47.21000114109]}}対象": "いいえ", "addr:housenumber": "35" }, "geometry": {"type": "Point", "coordinates": [-1.55076671448, 47.21000114109]}}

]}

外部 geojson ファイルをリーフレット マップにロードする方法で説明されていることを試しましたが、動作しません。

4

1 に答える 1

1

JSON が有効であっても、有効な GeoJSON オブジェクトを操作しているとは限りません。たとえば、{"foo": "bar"}完全に有効な JSON ですが、決して有効な GeoJSON オブジェクトではありません。L.GeoJSON、リーフレットの GeoJSON レイヤーは、FeatureCollection または機能を含む配列を想定しています。

有効な FeatureCollection:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "id": 1
        },
        "geometry": {
            "type": "Point",
            "coordinates": [0,0]
        }
    },{
        "type": "Feature",
        "properties": {
            "id": 2
        },
        "geometry": {
            "type": "Point",
            "coordinates": [1,1]
        }
    }]
}

または、機能を備えた配列のみ:

[{
    "type": "Feature",
    "properties": {
        "id": 1
    },
    "geometry": {
        "type": "Point",
        "coordinates": [0,0]
    }
},{
    "type": "Feature",
    "properties": {
        "id": 2
    },
    "geometry": {
        "type": "Point",
        "coordinates": [1,1]
    }
}]

(機能の単なる配列は有効な GeoJSON オブジェクトではありませんが、Leaflet は問題なくそれを処理することに注意してください)

これらを L.GeoJson レイヤーにロードするには、スクリプトで使用できるようにする必要があります。レイヤーを作成する前に、オブジェクトを簡単に宣言できます。例えば:

// Declare GeoJSON object
var geojson = {
    type: "FeatureCollection",
    features: [
        // Features here
    ]
}

// Create a new GeoJSON layer with geojson object
// And add to map (assuming your map instance is assigned to "map")
var layer = new L.GeoJSON(geojson).addTo(map);

しかし、多くの機能がある場合、それは非常に混乱します。データオブジェクトを別のファイルに配置する必要があるため、ロジックとデータを分離しておくことを常にお勧めします。たとえば、"geo.json" というファイルにオブジェクトが格納されているとします。次に、選択した XHR/AJAX ソリューションを使用してファイルをロードできます。次の例では jQuery を使用しています。

// Fetch geo.json file and assign the data to geojson variable
$.getJSON('geo.json', function (geojson) {
    // Create a new GeoJSON layer with GeoJSON object
    // And add to map (assuming your map instance is assigned to "map")
    var layer = new L.GeoJSON(geojson).addTo(map);
});

Plunker の実際の例: http://plnkr.co/edit/Mh8p4F?p=preview

于 2015-01-19T01:06:51.043 に答える