I'm using ImageMapType to render a custom image over the google maps (API V3). I would like to get the X, Y of a certain Lat/Lng on the map. This works fine for the center of the map (which is at Lat 0 and Lng 0) but does not work for the other parts of the map because the X,Y values are calculated according to the entire world map and not that section of the world that my custom map is overlayed onto.
How could I get the proper X, Y values on the picture ?
The algorithm I'm using for finding the X and Y is the one below:
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int mapWidth = 800;
int mapHeight = 450;
// get x value
double x = (mapWidth*(180+longitude)/360) % mapWidth+(mapWidth/2);
// convert from degrees to radians
double latRad = latitude * java.lang.Math.PI / 180;
// get y value
double mercN = java.lang.Math.log(java.lang.Math.tan((java.lang.Math.PI / 4)+( latRad / 2)));
double y = (mapHeight / 2)-( mapWidth * mercN/(2 * java.lang.Math.PI));
System.out.println("X : " + (x - (mapWidth / 2)) + " Y: " + y);