3

After some hunting, it seems that if I have an a geojson file, I NEED the data and map to be on a server in order to view this in OpenLayers because of the protocol class. I saw an example where I can basically read the file as a javascript object by inserting it into my code. But there must be a way to simple throw my .json file without a server - right? I was able to do it with leaflet... I'm a newbie at this so any help would be much appreciated!

3

If by "there must be a way to simple throw my .json file", you mean load your .json file on to your map as features...

You can do it like this:

vectorLayer = new OpenLayers.Layer.Vector("MyLayer");
map.addLayer(vectorLayer);

function handler(request) {

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

    vectorLayer.addFeatures(geojson_format.read(request.responseText));
}

var request = OpenLayers.Request.GET({
    url: "geojson.json",
    callback: handler
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.