php - 将纬度/经度转换为 X/Y 坐标

标签 php javascript maps coordinates proj4js

我有纽约州纽约市的纬度/经度值; 40.7560540,-73.9869510 和地球的平面图像,1000px × 446px。

我希望能够使用 Javascript 将纬度/经度转换为点将反射(reflect)位置的 X、Y 坐标。

因此图像左上角的 X,Y 坐标为; 289、111

注意事项:

  1. 不用担心使用什么投影的问题,自己制作 假设或按照你所知道的去做 可能有用
  2. X,Y可以是图像的任意一个 Angular
  3. 奖励积分 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/

相关文章:

php - 谷歌翻译工具包 API 错误 ("Multipart must have Atom and media part")

php - 如何为从数据库中获取的文件制作下载按钮?

javascript - Google map 事件监听器在 Javascript 'for' 循环中无法正常工作

javascript - 谷歌地图javascript禁用谷歌标志链接

go - go map 的内存高效实现?

php - 高效存储数据

php - 在 PHP 中对数组进行分组

javascript - 变异观察者不起作用

C# 相当于 JavaScript "OR assignment"

javascript - TypeScript 中的 JQuery 和 JQueryStatic 接口(interface)有什么区别?