[OpenLayers-Users] Getting featurecollection from GeoJSON?

Eric Lemoine eric.lemoine at camptocamp.com
Tue Oct 6 15:50:57 EDT 2009


On Fri, Oct 2, 2009 at 11:00 PM, Christian Schanz
<christian.schanz at gmail.com> wrote:
> Hello,
>
> I need a function that returns a featurecollection which is created
> from a GeoJSON.
> So I think that I need to do something like the following:
>
> function getGeoJSON() {
>     var featurecollection;
>     function handler(request) {
>         //TODO: What to do here?
>     }
>     var request = OpenLayers.Request.GET( {
>             url: "http://path/to/my/GeoJSON",
>             callback: handler
>     });
>     return featurecollection;
> }
>
> But how exactly should the handler look like?
> I have tried to add something like
>     featurecollection = request.responseText;
> to the handler but then I get no features returned at all.
>
> And as a second question: Should my GeoJSON generating server insert a
> mime-type at the beginning of the
> response or is this not needed?

I'd recommend using a protocol. Something like this:

var protocol = new OpenLayers.Protocol.HTTP({
    url: "url-to-geojson",
    format: new OpenLayers.Format.GeoJSON()
});

protocol.read({
    callback: function(response) {
        if(response.success()) {
            var features = response.features;
            console.log(features);
        } else {
            console.log("failure");
        }
    }
});


Hope this helps,

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com



More information about the Users mailing list