Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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