0

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.enter image description here

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);
Sergiu
  • 2,502
  • 6
  • 35
  • 57
  • Can you post the results of your println function? What is the ouput? – Micromega Sep 16 '13 at 22:58
  • The function outputs correct values for the center of the image ... because it is placed at Lat/Long 0/0 but the left/right bounds of the image are not accurate since it considers the X = 0 where the vertical line on the map is and not where the image ends. – Sergiu Sep 17 '13 at 05:36
  • Can you check my question if it fits? http://stackoverflow.com/questions/18838915/converte-lat-lon-to-pixel – Micromega Sep 17 '13 at 07:25

1 Answers1

0

I'm not sure, this will solve your prolem, but may be could help you a bit:

https://developers.google.com/maps/documentation/javascript/reference?hl=fr#Projection

Overnuts
  • 783
  • 5
  • 17
  • I've tried using the fromLatLngToPoint method but with no luck ... the points calculated by it were way off. – Sergiu Sep 16 '13 at 07:20
  • argh, I think things related to 'projection' in the doc could bring you to the success. good luck :-) – Overnuts Sep 16 '13 at 09:27