我已经设置了一个特定静态地图图像的最小经度和纬度值。该地图图像是某个国家的一个部分。
/**
* Maximum longitude value of the map
*/
private float mapLongitudeMax;
/**
* Minimum longitude value of the map
*/
private float mapLongitudeMin;
/**
* Maximum latitude value of the map
*/
private float mapLatitudeMax;
/**
* Minimum latitude value of the map
*/
private float mapLatitudeMin;
我有一个名为mapImage
的BufferedImage
。
我和我的朋友编写了一个方法,它接收longitude
和latitude
,并给出大致在地图上的X
和Y
位置,以便您可以在地图上绘制一些东西。
现在,如果我想在地图上移动我的鼠标,我希望它显示我鼠标位置的longitude/latitude
,这意味着我需要创建一个方法,将鼠标位置的X和Y转换为longitude
和latitude
,该方法应该与我的另一个方法相反。
这是我用于将地球坐标转换为图像X
和Y
的方法:
protected Location getCoordinatesByGlobe(float latitude, float longitude) {
/**
* Work out minimum and maximums, clamp inside map bounds
*/
latitude = Math.max(mapLatitudeMin, Math.min(mapLatitudeMax, latitude));
longitude = Math.max(mapLongitudeMin, Math.min(mapLongitudeMax, longitude));
/**
* We need the distance from 0 or minimum long/lat
*/
float adjLon = longitude - mapLongitudeMin;
float adjLat = latitude - mapLatitudeMin;
float mapLongWidth = mapLongitudeMax - mapLongitudeMin;
float mapLatHeight = mapLatitudeMax - mapLatitudeMin;
float mapWidth = mapImage.getWidth();
float mapHeight = mapImage.getHeight();
float longPixelRatio = mapWidth / mapLongWidth;
float latPixelRatio = mapHeight / mapLatHeight;
int x = Math.round(adjLon * longPixelRatio) - 3;// these are offsets for the target icon that shows.. eedit laterrr @oz
int y = Math.round(adjLat * latPixelRatio) + 3; //
// turn it up
y = (int) (mapHeight - y);
return new Location(x, y);
}
现在我想了一下,首先想到的是反过来做...所以我开始做了,但是遇到了问题,比如我不能在没有
longitude
或latitude
的情况下获取adjLon
或adjLat
的值,因此这不能简单地通过反转来完成。我对坐标系统完全不熟悉,所以有点困惑,但我正在努力理解。你有什么提示吗?
编辑(不可能吗?)
根据这个答案,你不能真正得到实际结果,因为地球不是平的,它无法用经度和纬度转换为平面地图,除非实现一个真正的数学算法来使其适应变化。
我的代码中有几个原因导致答案不可能精确:
1.由于上述原因 2.因为我的X、Y值是整数而不是浮点数。
那么我的问题是,如果用我的方法真的不可能吗?