Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenLayers: parsed GeoJSON points always display at coords(0 , 0)

this is the first time i use OpenLayers and i don't understand what i'm doing wrong.

I try to display a simple point parsed from GeoJSON. The data seems to be parsed correctly (i checked with the console) but whatever point i give, it always displays at a position i guess to be LonLat(0,0) on my vector layer.

What am i doing wrong ?

var map, baseLayer, placesLayer, geojsonParser ;
// data below have been simplified and reformated to enhance readability
var geojsonData = 
{
    "type":"Feature",
     "geometry":
     {
        "type":"Point",
        "coordinates":[-4.0280599594116,5.3411102294922]
     },
     "properties":
     {
        "id":273,
        "name":"ABIDJAN"
     }
};

$(document).ready(function(){

map = new OpenLayers.Map('map');
  baseLayer = new OpenLayers.Layer.OSM();
  placesLayer = new OpenLayers.Layer.Vector();

  geojsonParser = new OpenLayers.Format.GeoJSON();
  placesLayer.addFeatures(geojsonParser.read(geojsonData));

  map.addLayers([baseLayer,placesLayer]);
  map.setCenter(
    new OpenLayers.LonLat(-4, 5.3).transform(
      new OpenLayers.Projection("EPSG:4326"),
      map.getProjectionObject()
    ), 5
  );

}); // document ready
like image 771
m_x Avatar asked Dec 27 '22 18:12

m_x


1 Answers

This is the right solution:

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

source: https://gist.github.com/1118357

like image 95
dojob Avatar answered Mar 05 '23 02:03

dojob