javascript 将像素位置转换为纬度/经度,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7019101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:42:40  来源:igfitidea点击:

Convert pixel location to latitude/longitude & vise versa

javajavascriptactionscript-3geolocationlatitude-longitude

提问by Tyler Egeto

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.

我需要将纬度经度值转换为像素位置以及做相反的事情。我找到了许多从 lat/lng->pixel 开始的解决方案,但在相反方向上找不到任何解决方案。

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
  • 地图是固定大小的,没有缩放,没有瓦片。
  • 我不需要任何超级准确的东西,这并不重要。
  • 最好是墨卡托投影,但不是必需的。我实际上并没有显示结果。(任何二维投影)
  • 我不能依赖任何基于网络的 API,即:没有谷歌地图

A solution in almost any programming language would be fine, as long as it doesn't rely on any platform specific APIs.

几乎任何编程语言的解决方案都可以,只要它不依赖于任何特定于平台的 API。

This is an example of going from lat/lng->pixel:

这是一个从 lat/lng->pixel 开始的例子:

var y = Math.round(((-1 * lat) + 90) * (this.MAP_HEIGHT / 180));
var x = Math.round((lng + 180) * (this.MAP_WIDTH / 360));

采纳答案by Markcf

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.

对那个数学不是完全有信心,因为它是在我的脑海中完成的,但那些应该有效,只要确保先测试它们。