[OpenLayers-Users] How to get mouse position to Lat / Lon for SPERICAL Mercator

Christopher Schmidt crschmidt at metacarta.com
Fri Feb 6 14:26:50 EST 2009


On Fri, Feb 06, 2009 at 11:12:16AM -0800, brancusi wrote:
> 
> 
> Christopher Schmidt-2 wrote:
> > 
> > http://docs.openlayers.org/library/spherical_mercator#reprojecting-points-bounds
> > 
> 
> Thanks Christopher, I read that and was confused a bit by it. I hope
> Jennifer won't mind me posting the code fragment she kindly emailed directly
> to me - it solved the question directly and perhaps it might help others who
> are searching for the same answer;
> 
> map.events.register("mousemove", map, function(e) {
>   var pixel = new OpenLayers.Pixel(e.xy.x,e.xy.y);
>   var lonlat = map.getLonLatFromPixel(pixel);
>   var lonlatGCS =
> OpenLayers.Layer.SphericalMercator.inverseMercator(lonlat.lon, lonlat.lat);

Don't use this. Use:

  lonlatGCS = lonlat.transform(
    new OpenLayers.Projection("EPSG:900913"),
    new OpenLayers.Projection("EPSG:4326")
  );  

(The latter is likely to continue to be a supported method, and works
with all projections, not just spherical mercator.) 

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Users mailing list