[OpenLayers-Users] Adding a GeoJSON Layer
Paul Spencer
pagameba at gmail.com
Sun Mar 30 18:30:12 EDT 2008
On 30-Mar-08, at 6:14 PM, Bjorn Sandvik wrote:
>> On Sun, Mar 30, 2008 at 06:11:05PM +0100, Bjorn Sandvik wrote:
>>> / Hi,
> />>/
> />>/ I'm looking at possibilities of using OpenLayers for thematic
> mapping
> />>/ (thematicmapping.org), and I think GeoJSON might be a suitable
> format
> />>/ for choropleth maps.
> />>/
> />>/ I'm able to generate GeoJSON on the server using PHP, and my
> country
> />>/ polygons are rendered in OpenLayers when I'm pasting the
> GeoJSON code
> />>/ on: http://www.openlayers.org/dev/examples/vector-formats.html
> />>/
> />>/ My question: What is the best way to load and render a GeoJSON
> layer in
> />>/ OpenLayers?
> />
>> new OpenLayers.Layer.GML("My GeoJSON Layer", "/file.geojson",
>> {format: OpenLayers.Format.GeoJSON});
>>
>> Regards,
>> --
>> Christopher Schmidt
>> MetaCarta
>
> Thanks, it works. Is it also possible to load a GeoJSON file using
> the read function?
>
> map = new OpenLayers.Map('map');
> var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
> "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'});
> vectors = new OpenLayers.Layer.Vector("Vector Layer");
> map.addLayers([wms, vectors]);
>
> var parser = new OpenLayers.Format.GeoJSON();
> var features = parser.read("filename");
> ...
>
> See last line.
> I guess I can more easily manipulate the features this way.
You'll need to have something load the remote file, you could use
something like:
OpenLayers.loadURL(url, {}, null, function(r) {
var p = new OpenLayers.Format.GeoJSON();
var f = p.read(r.responseText);
vectors.addFeatures(f);
});
or break into functions
OpenLayers.loadURL(url, {}, null, onLoad, onLoadFailed);
function onLoad(r) {
if (r.status == 200) {
var p = new OpenLayers.Format.GeoJSON();
var f = p.read(r.responseText);
vectors.addFeatures(f);
}
}
Cheers
Paul
function onLoadFailed(r) {
//something went wrong
}
>
>
> Bjorn
>
> http://blog.thematicmapping.org
>
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
More information about the Users
mailing list