Hi,

In my java app I need to calculate coordinates for the google tile
coordinate system.

I basically have two use cases :

1. get the XY pixel coordinates at zoom for a given latitude /
longitude
2. get the latitude / longitude coordinates for a given XY pixel
coordinate at a given zoom



Here's the code I'm currently using :


        //returns a point that is a google pixel reference for the particular
lat/lng and zoom
        public static Point latLngToAbsolutePixelCoordsAtZoom(double lat,
double lon, int zoom, int tileSize){

                //normalize

                // first convert to Mercator projection
        // first convert the lat lon to mercator coordintes.
        if (lon > 180)
        {
            lon -= 360;
        }

        lon /= 360;
        lon += 0.5;
        lat = 0.5 - ((Math.log(Math.tan((Math.PI / 4) + ((0.5 *
Math.PI * lat) / 180))) / Math.PI) / 2.0);


        //now that we have the normalized coords
        double scale = (1 << zoom) * tileSize;

        Point ret = new Point((int)( lon *scale), (int)(lat* scale));

        return ret;

        }





        //PointD is a XY point using java double primitive
        public static PointD pixelToLatLong(int pixelX, int pixelY, int zoom)
{


        double longitude = (((double)pixelX * 360) / (256 * Math.pow
(2, zoom))) - 180;

        double efactor = Math.exp((0.5 - (double)pixelY / 256 /
Math.pow(2, zoom)) * 4 * Math.PI);

        double latitude = Math.asin((efactor - 1) / (efactor + 1)) *
180 / Math.PI;



        return new PointD(longitude, latitude);
    }





You can test this code as follow :


private void runTest(){

                System.out.println("initial values = lat = 46.20181075433888 " 
);
                System.out.println("initial values = lon = 6.140263080596924 " 
);


                Point pixels = 
CoordinateConverter.latLngToAbsolutePixelCoordsAtZoom
(46.20181075433888, 6.140263080596924, 16, 256);

                System.out.println("POINT PIXEL X = " + pixels.getX());
                System.out.println("POINT PIXEL Y = " + pixels.getY());

                PointD latlon = 
CoordinateConverter.pixelToLatLong(pixels.getX(),
pixels.getY(), 16);

                System.out.println("latlon lon = " + latlon.getX());
                System.out.println("latlon lat = " + latlon.getY());
        }





My problem is that the results of both methods don't match ...
The latitude which was 46.20181075433888 initially, is outputed
46.2018147146228 by my pixelToLatLong method.
I must be doing some miscalculation somewhere but I don't know where.


Can anybody help me ?


thanks a lot !

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to