将像素位置转换为经度/纬度&反之亦然 [英] Convert pixel location to latitude/longitude & vise versa
问题描述
我需要将纬度经度值转换为像素位置,并做相反的处理。我发现很多解决方案都是从lat / lng-> pixel,但是找不到任何相反的东西。
几个注释:
- 地图是一个固定的大小,没有缩放,没有瓷砖。
- 我不需要任何超精确的,它并不重要。
- 优选mercator投影,但不是必需的。我实际上并没有显示结果。 (任何2D投影)
- 我不能依赖任何基于web的API,即:没有Google Maps
几乎任何编程语言的解决方案都可以,只要它不依赖任何平台特定的API即可。
这是从(lat / lng-> pixel):
var y = Math.round((( - 1 * lat)+90) *(this.MAP_HEIGHT / 180));
var x = Math.round((lng + 180)*(this.MAP_WIDTH / 360));
var y =(Math.round((( - 1 * lat)+90)*(this.MAP_HEIGHT / 180));
var x = Math.round((lng + 180)*(this.MAP_WIDTH / 360));
使用一些代数,我出来了:
var lat =(y /(this.MAP_HEIGHT / 180)-90)/ - 1
var lng = x /(this.MAP_WIDTH / 360)-180
在这个数学计算中不是完全有信心,因为它是在我脑海中完成的,但是这些应该可以工作,只要确保先测试他们。
I need to convert latitude longitude values into pixel positions as well as do the opposite. I've found many solutions to go from lat/lng->pixel, but can't find anything on the reverse.
A couple of notes:
- The map is a fixed size, no zooming, no tiles.
- I don't need anything super accurate, its not important.
- Preferably mercator projection, but not required. I'm not actually display the result. (Any 2D projection)
- I can't rely on any web based API's, ie: no Google Maps
A solution in almost any programming language would be fine, as long as it doesn't rely on any platform specific APIs.
This is an example of going from lat/lng->pixel:
var y = Math.round(((-1 * lat) + 90) * (this.MAP_HEIGHT / 180));
var x = Math.round((lng + 180) * (this.MAP_WIDTH / 360));
var y = Math.round(((-1 * lat) + 90) * (this.MAP_HEIGHT / 180));
var x = Math.round((lng + 180) * (this.MAP_WIDTH / 360));
Use some algebra and I came out with:
var lat=(y/(this.MAP_HEIGHT/180)-90)/-1
var lng = x/(this.MAP_WIDTH/360)-180
Not completely confident in that math since it was done in my head, but those should work, just make sure to test them first.
这篇关于将像素位置转换为经度/纬度&反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!