问题
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:
- X,Y coordinates from the Map
- Total width and height from the Map
- 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:
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());
来源:https://stackoverflow.com/questions/60827507/convert-cartesian-x-y-to-coordinates-gps-latitude-longitude