我有纽约州纽约市的纬度/经度值; 40.7560540,-73.9869510 和地球的平面图像,1000px × 446px。
我希望能够使用 Javascript 将纬度/经度转换为点将反射(reflect)位置的 X、Y 坐标。
因此图像左上角的 X,Y 坐标为; 289、111
注意事项:
- 不用担心使用什么投影的问题,自己制作 假设或按照你所知道的去做 可能有用
- X,Y可以是图像的任意一个 Angular
- 奖励积分 PHP 中的相同解决方案(但我 真的需要 JS)
最佳答案
您使用的投影将改变一切,但这将在假设墨卡托投影的情况下起作用:
<html>
<head>
<script language="Javascript">
var dot_size = 3;
var longitude_shift = 55; // number of pixels your map's prime meridian is off-center.
var x_pos = 54;
var y_pos = 19;
var map_width = 430;
var map_height = 332;
var half_dot = Math.floor(dot_size / 2);
function draw_point(x, y) {
dot = '<div style="position:absolute;width:' + dot_size + 'px;height:' + dot_size + 'px;top:' + y + 'px;left:' + x + 'px;background:#00ff00"></div>';
document.body.innerHTML += dot;
}
function plot_point(lat, lng) {
// Mercator projection
// longitude: just scale and shift
x = (map_width * (180 + lng) / 360) % map_width + longitude_shift;
// latitude: using the Mercator projection
lat = lat * Math.PI / 180; // convert from degrees to radians
y = Math.log(Math.tan((lat/2) + (Math.PI/4))); // do the Mercator projection (w/ equator of 2pi units)
y = (map_height / 2) - (map_width * y / (2 * Math.PI)) + y_pos; // fit it to our map
x -= x_pos;
y -= y_pos;
draw_point(x - half_dot, y - half_dot);
}
</script>
</head>
<body onload="plot_point(40.756, -73.986)">
<!-- image found at http://www.math.ubc.ca/~israel/m103/mercator.png -->
<img src="mercator.png" style="position:absolute;top:0px;left:0px">
</body>
</html>
关于php - 将纬度/经度转换为 X/Y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1019997/