【发布时间】:2016-09-14 00:04:51
【问题描述】:
我已设置特定静态地图图像的最小经度和纬度值。那张地图图片是某个国家/地区的截图。
/**
* 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);
}
现在我试着想,我脑海中的第一个想法就是反过来做同样的事情......所以我开始这样做,但遇到了一些问题,比如我无法获得 adjLon 的值或adjLat 没有longitude 或latitude,所以这不能简单地通过反转它来完成。我是坐标系统的新手,所以对我来说有点困惑,但我开始赶上它。
有什么建议吗?
编辑(不可能?)
根据this answer,你不能真正得到真正的结果,因为地球不是平的,如果不实施真正的数学算法使其工作,它就无法真正转换为经纬度的平面地图变化。
我的代码中有几个原因导致答案不能准确:
- 由于上述原因
- 因为我的 X、Y 值是整数而不是浮点数。
所以我现在的问题是,如果我的方法真的不可能吗?
【问题讨论】:
-
您的应用程序是做什么用的?如果您尝试将其用于估计,则可以进行一些舍入等。
-
您可以通过使用对您的应用程序具有足够准确性的模型来获得对您的应用程序来说“足够好”的结果。一般来说,这将取决于您将鼠标悬停在图像上的像素:度数比。有很多很多不同的方法可以“展开”球体。我提到的一个是最受欢迎的之一,但还有其他的。这应该让你开始:en.wikipedia.org/wiki/List_of_map_projections