[OpenLayers-Users] Re: Example of GeoJson fetch via HTTP?

Scott Chapman scott at mischko.com
Tue Feb 28 17:41:21 EST 2012


Here's how you do this:

        var vector_layer = new OpenLayers.Layer.Vector();
        map.addLayer(vector_layer);

        OpenLayers.Request.GET({
               url: "/kiosks_status_geoJSON/",
               headers: {'Accept':'application/json'},
               success: function (req)
                        {
                        var g = new OpenLayers.Format.GeoJSON();
                        var feature_collection = g.read(req.responseText);
                        vector_layer.destroyFeatures();
                        vector_layer.addFeatures(feature_collection);
                        }
           });

If anyone has any pointers, I'd be happy to hear them!

Scott


On Mon, Feb 27, 2012 at 7:36 PM, Scott Chapman <scott at mischko.com> wrote:
> I've got a map and want to add a layer to it from a bunch of GeoJSON
> formatted features that I can fetch from the server at
> /kiosks_status_geoJSON/.  The server will return JSON format output.
>
> Can someone point me to an example of how to do this or lay it out for me?
>
> Some of my points of confusion include:
>
> 1) The docs (http://dev.openlayers.org/releases/OpenLayers-2.11/doc/apidocs/files/OpenLayers/Format/GeoJSON-js.html)
> say that a GeoJSON Format.read() will deserialize a GeoJSON string,
> but the example
> (http://dev.openlayers.org/releases/OpenLayers-2.11/examples/geojson.html)
> shows a GeoJSON.read() being passed an actual javascript object that's
> already been deserialized.  Is it smart enough to deal with both
> cases?
>
> 2) The overall flow of things.   I think I should make a Request.GET
> to fetch the URL, an event handler to deal with the response having a
> Format.geoJSON.read() the result and vector_layer.addFeatures on that.
>  I'd love to see an example of this setup.
>
> Thanks!
> Scott


More information about the Users mailing list