Convert pixel location to latitude/longitude & vise versa

穿精又带淫゛_ 提交于 2019-12-04 12:33:31

问题


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));

回答1:


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.



来源:https://stackoverflow.com/questions/7019101/convert-pixel-location-to-latitude-longitude-vise-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!