我设置了特定静态地图图像的最小经度和纬度值.那张地图图片是某个国家的剪影.
/**
* 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.
我有一个我和朋友一起写的方法,它接收经度和纬度,并在地图上给你一个X和Y位置,这样你就可以在地图上绘制一些东西.
现在,如果我想在地图上移动鼠标,我希望它显示我的鼠标位置的经度/纬度,这意味着我需要创建一个方法,将鼠标位置的X和Y转换为经度和纬度,这应该做与我的另一种方法相反.
这是我将地球坐标转换为图像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);
}
现在我试着思考,第一个进入脑海中的想法就是反过来做同样的事情…所以我开始这样做,我遇到了类似的问题,我没有经度就得到adjLon或adjLat的值或纬度,所以这不能简单地通过反转来完成.我对坐标系统都很陌生,所以对我来说有点混乱,但我开始追上它.
有什么提示吗?
编辑(不可能?)
根据this answer,你不可能真正得到真实的结果,因为地球不是平坦的,它不能真正转换成具有经度和纬度的平面地图而不实施真正的数学算法以使其适应变化.
我的代码中有几个原因导致答案不准确:
>由于上述原因
>因为我的X,Y值是整数而不是浮点数.
所以现在我的问题是,如果我的方法真的不可能吗?