Wie lese ich eine externe GeoJSON-Datei von Openlayern?

Ich muss einige Linien von OpenLayers zeichnen. Die Linienmerkmale sind im GeoJSON-Format codiert. Mein Code ist für hartcodierte GeoJSON-Funktionen in Ordnung. Aber wenn ich diese Funktionen in eine separate Datei lege und versuche, sie zu laden. Es geht einfach nicht. Ich weiß nicht, woran es liegt, wenn ich eine externe GeoJSON-Datei lade. Ich habe beiden den Code gegeben.

Code 1:

<code>// This code is ok with hard coded GeoJSON features
map.addControl(new OpenLayers.Control.LayerSwitcher());

            vectorLayer = new OpenLayers.Layer.Vector("Lines");

            var myGeoJSON = { "type": "FeatureCollection",
                "features": 
                [

                    { "type": "Feature", "properties": { "LENGTH": 756.304000}, "geometry": { "type": "LineString", "coordinates": [ [ 18.105018, 59.231027 ], [ 18.104176, 59.230737 ], [ 18.103928, 59.230415 ], [ 18.103650, 59.230336 ], [ 18.103028, 59.230463 ], [ 18.102491, 59.230418 ], [ 18.101976, 59.230237 ], [ 18.100893, 59.230110 ], [ 18.100117, 59.230016 ], [ 18.097715, 59.230262 ], [ 18.096907, 59.230376 ], [ 18.096637, 59.230405 ], [ 18.096578, 59.230428 ], [ 18.096429, 59.230450 ], [ 18.096336, 59.230479 ], [ 18.096108, 59.230534 ], [ 18.095971, 59.230600 ], [ 18.095925, 59.230633 ], [ 18.095891, 59.230665 ], [ 18.094000, 59.231676 ], [ 18.093864, 59.231720 ] ] } }
                    ,                   
            { "type": "Feature", "properties": { "LENGTH": 1462.390000}, "geometry": { "type": "LineString", "coordinates": [ [ 17.877073, 59.461653 ], [ 17.877116, 59.461598 ], [ 17.876936, 59.461507 ], [ 17.876936, 59.461323 ], [ 17.876773, 59.461098 ], [ 17.876430, 59.460885 ], [ 17.876413, 59.460553 ], [ 17.876576, 59.460280 ], [ 17.876575, 59.460078 ], [ 17.876762, 59.460060 ], [ 17.877371, 59.460042 ], [ 17.877808, 59.460046 ], [ 17.878641, 59.460046 ], [ 17.879010, 59.460078 ], [ 17.879337, 59.460044 ], [ 17.879526, 59.459878 ], [ 17.879749, 59.459563 ], [ 17.880058, 59.459538 ], [ 17.880435, 59.459503 ], [ 17.887550, 59.453608 ], [ 17.887696, 59.453430 ], [ 17.887971, 59.453150 ], [ 17.888221, 59.452843 ], [ 17.888246, 59.452721 ], [ 17.888435, 59.452609 ], [ 17.888470, 59.452568 ], [ 17.888517, 59.452410 ] ] } }

                ]
            };

            var geojson_format = new OpenLayers.Format.GeoJSON({
                'internalProjection': map.baseLayer.projection,
                'externalProjection': new OpenLayers.Projection("EPSG:4326")
            });


            map.addLayer(vectorLayer);

            vectorLayer.addFeatures(geojson_format.read(myGeoJSON));

            map.setCenter(
                new OpenLayers.LonLat(18.068611, 59.329444).transform(
                    new OpenLayers.Projection("EPSG:4326"),
                    map.getProjectionObject()
                ), 10
            );
</code>

Code 2: Dieser Code zeigt einen Fehler an, bei dem Features nicht geladen werden konnten

<code>//This code does not work because it can not load the external GeoJSON file

map.addControl(new OpenLayers.Control.LayerSwitcher());

vectorLayer = new OpenLayers.Layer.Vector("Lines");

var myGeoJSON = new OpenLayers.Layer.Vector("Lines", {
    strategies: [new OpenLayers.Strategy.Fixed()],                
    protocol: new OpenLayers.Protocol.HTTP({
           url: "ml/lines.json"

           })
     });

var geojson_format = new OpenLayers.Format.GeoJSON({
    'internalProjection': map.baseLayer.projection,
    'externalProjection': new OpenLayers.Projection("EPSG:4326")
    });

map.addLayer(vectorLayer);

vectorLayer.addFeatures(geojson_format.read(myGeoJSON));

map.setCenter(
     new OpenLayers.LonLat(18.068611, 59.329444).transform(
         new OpenLayers.Projection("EPSG:4326"),
             map.getProjectionObject()
                ), 10
            );
</code>

Danke im Voraus

Antworten auf die Frage(1)

Ihre Antwort auf die Frage