I'm trying to get coordinates from a mouse click on the map.
The coordinates are right until I scroll the map. After that they are wrong - they are shifted from the position of the click. If I change the zoomlevel, next coordinates are right until I scroll the map again.
First I thought getLayerPxFromViewPortPx
returns wrong value, because if I scroll to the left and to the top and click there it gives me a pixel with negative x
and y
. But I suppose it is the way it works and x
and y
are relative to the layers center.
Here is what I have:
map.events.register("click", map, function(e){ var opx = map.getLayerPxFromViewPortPx(e.xy) ; var lonlat = map.getLonLatFromPixel(opx); console.log(lonlat) var marker = new OpenLayers.Marker(lonlat); markers.addMarker(marker)
What else could be failing?
var lonlat = map.getLonLatFromViewPortPx(e.xy)
and now it seems working. However I'm still curious why did the previous fail. About the complete example, you can find it here: dev.ivanatora.info/spirki – Ivan Petrushev