【问题标题】:lat long in coordinates in map php地图php中的坐标经纬度
【发布时间】:2014-01-07 10:39:51
【问题描述】:
<?php
header("Content-type:image/png");
$image1 = imagecreatefrompng('cylindrical_map.png'); 
$image2 = imagecreatefrompng('pin.png'); 

$long = 18.96;
$lat = 72.82;
$scale_x = imagesx($image1);
$scale_y = imagesy($image1);
$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);

imagecopymerge($image1, $image2, $pt["x"],$pt["y"], 0, 0, 80, 80, 98); //// place a second image on top of image 1


imagepng($image1);

function getlocationcoords($lat, $lon, $width, $height)
{  
    $x = (($lon + 180) * ($width / 360));
    $y = ((-1 * $lat)+90) * ($height / 180);
    return array("x"=>round($x),"y"=>round($y));
}

?>

我有这段代码可以将纬度和经度转换为坐标,但例如我把纽约市的纬度和经度放在正确的位置,在将纬度和经度转换为坐标时遇到问题。

请帮帮我。

感谢任何帮助。

【问题讨论】:

  • 您需要它有多准确?行星的大小和形状有不同的基准。
  • 看看这个 SO 问题:stackoverflow.com/questions/8175265/… 它是 C#,但接受的答案中的链接显示了如何转换坐标。
  • 尽可能准确..地图大小为700 x 500..
  • 了解地图的形状非常重要,因为计算方式不同。如果它是等角投影,则非常简单。 bit.ly/19DJATK其他预测:en.wikipedia.org/wiki/List_of_map_projections
  • 地图的投影是Equirectangular cylinder_map ..

标签: php php-gd


【解决方案1】:

您可以使用PEAR packagethis SO answer 来计算您的坐标。

SO 线程包含满足您需求的代码。


最后,我为您提供了一个工作示例。 (花了几分钟)。 我想你拿了this 的文章并稍作改动。

首先,您需要计算引脚的偏移量。我从一个 jQuery 插件中获取了this one(我将它缩小到 50px 宽度)。这意味着您从尖端获取 x 和 y 坐标。请注意,必须以左上角为原点!

$pinOffsetX = 19;
$pinOffsetY = 42;

现在你写下你的坐标。我拿了纽约市的坐标。

$long = -74.0059700;
$lat = 40.7142700;

加载您的图片。我拿this map 做了一张大图,检查准确度。

$pin = imagecreatefrompng("pin.png");
$im = imagecreatefromjpeg("map.jpg");

获取图像的大小并计算坐标

$scale_x = imagesx($im);
$scale_y = imagesy($im);
$pinWidth = imagesx($pin);
$pinHeight = imagesy($pin);
$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);

功能没有改变。 现在将图钉复制到地图上。注意:我使用 imagecopy 而不是 imagecopymerge。

imagecopy($im, $pin, $pt["x"] - $pinOffsetX, $pt["y"] - $pinOffsetY, 0, 0, $pinWidth, $pinHeight);

必须减去销偏移才能将尖端移动到该位置。 输出图像(并销毁它!)

header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

【讨论】:

  • 我已经看过了,所以问题出在哪里--> $long_at_left=//Longitude of left-hand coordinates $long_at_right=//Longitude of right-hand coordinates $lat_at_left=//Latitude of left-hand coordinates $lat_at_right=//Latitude of right-hand coordinates
  • 希望我的例子有所帮助。
  • 非常感谢。我真的很感谢你的帮助....我可以复制整个代码吗?..如果你不介意:P..再次感谢..
  • 我可以用图钉和地图查看您的图像输出吗?
  • 这是带有截图的完整包:dropbox.com/s/oc2r4ki5c588kds/map_projection.zip
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多