Search code examples
javascriptcoordinatesopenlayersgeojson

OpenLayers 2, GeoJSON cant get coordinates to work


I'm trying to display some Polygons on a my map, I'm new in whole OpenLayers maps geojson thing so be gentle please :).

First what work:

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 show up on server, at 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 dont 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 display polygon on a different place even though projection is specified in same way(or atleast for me...).


Solution

  • 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.