Here are the formulas we use to get from web mercator to lon lat and vice versa. We simple
did a copy/paste from our source code. tmvector2d is just a simple structure containing
two values:
So here are the two function. Note lon lat are always in radians.
Code
tmvector2d LonLatFromWebMercator( const vector2d webmercator )
{
vector2d lonlat;
lonlat.x = ( webmercator.x * 2.0 * PI / 131072.0 ) - PI;
lonlat.y = webmercator.y - 65536.0;
lonlat.y *= -2.0 * TM_PI / 131072.0;
lonlat.y = exp( lonlat.y );
lonlat.y = atan( lonlat.y );
lonlat.y = 2.0 * ( lonlat.y - PI / 4.0 );
return lonlat;
}
vector2d WebMercatorFromLonLat( const vector2d lonlat )
{
vector2d webmercator;
webmercator.y = ( 0.5*lonlat.y + TM_PI / 4.0 );
webmercator.y = tan( webmercator.y );
webmercator.y = log( webmercator.y );
webmercator.y *= 131072.0 / ( -2.0*TM_PI );
webmercator.y = webmercator.y + 65536.0;
webmercator.x = ( lonlat.x + TM_PI ) * ( 131072.0 / ( 2.0*TM_PI ) );
return webmercator;
}
Display More
We strongly recommend to use web mercator for now as lon lat adds some additional complexity. We do support lon lat as well, but we have to create a sample first.