0

I need to convert coordinates X, Y into Latitude and Longitude. I've read wikipedia map projections, similar stackoverflow forums, applied my custom solutions and still didn't work.

The coordinates I get from my formula are wrong, Longitude is acceptable but Latitude is not. I don't see where my calculous is wrong.

The X,Y Point is taken from a JLayeredPane with the size of a background Map image, once a smaller image is released on this Map image, the point is taken.

public void mouseReleased(MouseEvent e) {               
    DM.setUpCoordinates(layeredPane.getComponent(index-1).getLocation());       
}

After this, I'm trying to correctly calculate the Latitude and Longitude projection. The data I own is:

  1. X,Y coordinates from the Map
  2. Total width and height from the Map
  3. Latitude and Longitude where the map is centered

What I have tryied so far:

Trying Equirectangular projection

public void setUpCoordinates(Point p) {     
    //Equirectangular projection: optimal for small streets 
    Long = ((p.getX())/(6371000*Math.cos(MG.getLati())))+MG.getLongi(); 
    Lat = (((p.getY())/6371000)+MG.getLati());
}

I also tryied to implement the Mercator projection from this link with very little to no success at all.

I am aware I'm not using total width and height from the Map on my formulas and this might be the error, but I don't know how to use it!

any help how to convert from (x,y) to (latitude, longitude)?

Thanks,

1 Answers1

0

You need to shift pixel coordinates by center position and also use map scale - longitude and latitude range (I assume that elongation Math.cos(MG.getLati()) coefficient is accounted in longitude scale)

Long = (p.getX() - MapWidth/2)*LongitudeRange/MapWidth  +MG.getLongi(); 
Lat = (p.getY() - MapHeight/2)*LatitudeRange/MapHeight + MG.getLati());
MBo
  • 77,366
  • 5
  • 53
  • 86
  • First of all thx for answering. My Lat goes from -90 to 90 and my Long -180 to 180, but this formula is not working, let's see an example: clickedXY: (409,330) Map:(1280,700) central coords: lat:41.469999999999985 long:2.0699999999999985 ,solution: lat:41.470 long:2.051---------------------------- `Long = (p.getX()-MapWidth/2)*LongitudeRange/MapWidth+MG.getLongi(); ` -------------------------- **Long = (409-(1280/2))*(360/1280)+2.0699999999999985 = -62.89 =! 2.051**----------------------- `Lat = (p.getY()-MapHeight/2)*LatitudeRange/MapHeight+MG.getLati());` **Lat = 36.327 != 41.470** – GuillemGarciaSabate Mar 24 '20 at 17:10
  • Emm... You see all the Earth surface at the screen just now?? I think you must use real value for the degree width of visible part of map, not all 360 range. – MBo Mar 25 '20 at 02:35
  • Hi, I corrected my error and it worked, I was using the wrong Lat&Longrange as you said, thank you so much, I upvoted you but I don't have reputation so it's not visible – GuillemGarciaSabate Mar 26 '20 at 15:11
  • @Ravi Kumar Ravanam I don't understand what X and Y mean there – MBo May 27 '20 at 07:28
  • Your question should contain all the needed information. For now it is not clear. – MBo May 27 '20 at 07:47
  • What if the map is not pointed to the north? It's rotate by some angle? I don't think this will work. Right? – Bruno Carneiro Dec 15 '22 at 14:41
  • @Bruno Carneiro Of course, this is different problem – MBo Dec 16 '22 at 03:04