Julien Guigner
Julien Guigner

Reputation: 193

Openlayers - Projection issues when getting latitude/longitude from a point

I'm trying to get the latitude/longitude from a draggable marker with Openlayers and OSM but I can't find the good settings for the projection conversion, what I am doing wrong ?

Here is the code: http://pastie.org/2300321 (see addMarker l140 & updateTargets l153) & and a little demo test.

If you submit an address, then drag the marker, the longitude and latitude are wrong. I tested a few different projections but I'm not sure what I've to use…

Upvotes: 2

Views: 4374

Answers (1)

igorti
igorti

Reputation: 3856

I think the problem is inside updateTargets method:

var point = this.feature.geometry;
var pixel = new OpenLayers.Pixel(point.x, point.y);
var coord = this.map.getLonLatFromPixel(pixel).transform(
   new OpenLayers.Projection("EPSG:900913"),
   new OpenLayers.Projection("EPSG:4326")
);

this.feature.geometry is already specified in lon/lat coordinates, not in pixels. So I suggest that you skip second line and do the conversion from OpenStreetMap projection to lon/lat directly on geometry object:

var coord = this.feature.geometry.transform(
   new OpenLayers.Projection("EPSG:900913"),
   new OpenLayers.Projection("EPSG:4326")
);

Upvotes: 5

Related Questions