1

I'm trying to display some polygons on a map, and I'm new to OpenLayers, maps and GeoJSON.

First what works:

vector1 = new OpenLayers.Layer.Vector("GeoJSON1",
{
    projection       : "EPSG:4326",
    strategies       : [new OpenLayers.Strategy.Fixed()],
    protocol         :  new OpenLayers.Protocol.HTTP({
                            url: "test.php",
                            format: new OpenLayers.Format.GeoJSON()
                        })
});

This piece of code works, the GeoJSON from test.php shows up on the server, at the position where it should be. But GeoJSON I try to reach is on another server and it throw errors (I know I can set headers and it would work) but I don't want to do it this way.

This doesn't work:

var geojs_format = new OpenLayers.Format.GeoJSON();
var geojsval= {
                "type": "Polygon", 
                "coordinates": 
                    [[[11.0878902207, 45.1602390564], 
                      [14.931640625, 40.9228515625], 
                      [0.8251953125, 41.0986328125], 
                      [7.63671875, 48.96484375], 
                      [11.0878902207, 45.1602390564]]]
            };
vector = new OpenLayers.Layer.Vector("GeoJSON",{projection:"EPSG:4326"});
vector.addFeatures(geojs_format.read(geojsval));

This does not work - It displays polygon at a different place even though projection is specified in same way (or at least for me...).

  • 1
    This is just a guess, since I'm not really familiar with OpenLayers either, but are the coordinates within your GeoJSON ordered as [lon, lat] or [lat, lon]? GeoJSON coordinates are supposed to be specified as [lon, lat], and one common reason for things to display in the wrong place is if they are entered as [lat, lon]. Switching the order here shows a triangle over Ethiopia and Somalia. – nathansnider Nov 14 '15 at 5:38
0

I solved this. When creating OpenLayers.Format.GeoJSON you can specific projection like this...

var geojs_format = new OpenLayers.Format.GeoJSON({
            'internalProjection': new OpenLayers.Projection("EPSG:900913"),
            'externalProjection': new OpenLayers.Projection("EPSG:4326")
        });

This solved the problem.

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.