需要帮助使用来自php值的javascript计算经度和纬度中点

时间:2011-04-14 00:16:37

标签: php javascript

我目前正在开展一个项目,并遇到了很多问题。 我正在使用谷歌地图API运行地图应用程序,我需要计算两点之间的中点,即4个总点数(纬度/经度),它们存储为来自用户的php值。

我目前使用的公式需要将以度为单位的值转换为弧度。 然后我需要将它们转换回度数以插入我的代码的谷歌映射部分。

我需要计算这一点,这样我就可以将它显示为地图的中间位置,以便它能整齐地显示在中心位置。

这是我目前的代码:

<?php
ob_start();
?>

<?php

$from = $_POST["from"];
$to = $_POST["to"];

$connection = mysql_connect("localhost", "dlazett", "PASSWORD");

mysql_select_db("dlazett", $connection);

$f = mysql_query ("SELECT * FROM capstone WHERE id = '$from' ",
$connection);

$wfrom = mysql_fetch_array($f);

$latf = $wfrom["lat"];
$lonf = $wfrom["lon"];

$t = mysql_query ("SELECT * FROM capstone WHERE id = '$to' ",
$connection);

$wto = mysql_fetch_array($t);

$latt = $wto["lat"];
$lont = $wto["lon"];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<title>UT Campus Compass</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name = "viewport" content = "width = device-width">
<meta name="apple-mobile-web-app-capable" content="yes" />

<script src="http://maps.google.com/maps?file=ap…
type="text/javascript"></script>
<script type="text/javascript">
function midpoint() {
var lat2 = (<?php echo "$latf";?>) * (Math.PI/180);
var lat1 = (<?php echo "$latt";?>) * (Math.PI/180);
var dLon = (<?php echo "$lonf";?>-<?php echo "$lont";?>) * (Math.PI/180);
var Bx = Math.cos(lat2) * Math.cos(dLon);
var By = Math.cos(lat2) * Math.sin(dLon);
var lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2)…
Math.sqrt( (Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By) );
var lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
var lat4 = lat3 * (180/Math.PI);
var lon4 = lon3 * (180/Math.PI);
}

function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canva…
map.setCenter(new GLatLng(lat4, lon4), 16);
var polyline = new GPolyline([
new GLatLng(<?php echo "$latf";?>, <?php echo "$lonf";?>),
new GLatLng(<?php echo "$latt";?>, <?php echo "$lont";?>)
], "#ff0000", 10);
map.addOverlay(polyline);
}
}
</script>
</head>

<body onload="initialize()" onunload="GUnload()">
<center>
<div id="map_canvas" style="width: 425px; height: 600px"></div>
<div id="message"></div>
</body>
</html>

<?php
ob_end_flush();
?>

非常感谢任何帮助。

谢谢, 丹

4 个答案:

答案 0 :(得分:2)

PHP中的

function midpoint ($lat1, $lng1, $lat2, $lng2) {

    $lat1= deg2rad($lat1);
    $lng1= deg2rad($lng1);
    $lat2= deg2rad($lat2);
    $lng2= deg2rad($lng2);

    $dlng = $lng2 - $lng1;
    $Bx = cos($lat2) * cos($dlng);
    $By = cos($lat2) * sin($dlng);
    $lat3 = atan2( sin($lat1)+sin($lat2),
    sqrt((cos($lat1)+$Bx)*(cos($lat1)+$Bx) + $By*$By ));
    $lng3 = $lng1 + atan2($By, (cos($lat1) + $Bx));
    $pi = pi();
    return ($lat3*180)/$pi .' '. ($lng3*180)/$pi;
}

答案 1 :(得分:1)

function distance($lat1, $lon1, $lat2, $lon2, $unit) { 

  $theta = $lon1 - $lon2; 
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
  $dist = acos($dist); 
  $dist = rad2deg($dist); 
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344); 
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}

此功能可以帮助您

答案 2 :(得分:0)

使用Openlayers,这有助于实现这一目标。 http://www.openlayers.org/

答案 3 :(得分:0)

对于javascript版本,我转换了@ vineesh的答案。

function midpoint ($lat1, $lng1, $lat2, $lng2) {
    $lat1 = $lat1 * 0.017453292519943295;
    $lng1 = $lng1 * 0.017453292519943295;
    $lat2 = $lat2 * 0.017453292519943295;
    $lng2 = $lng2 * 0.017453292519943295;

    $dlng = $lng2 - $lng1;
    $Bx = Math.cos($lat2) * Math.cos($dlng);
    $By = Math.cos($lat2) * Math.sin($dlng);
    $lat3 = Math.atan2( Math.sin($lat1)+Math.sin($lat2),
    Math.sqrt((Math.cos($lat1)+$Bx)*(Math.cos($lat1)+$Bx) + $By*$By ));
    $lng3 = $lng1 + Math.atan2($By, (Math.cos($lat1) + $Bx));
    $pi = 3.141592653589793;
    $lat = ($lat3*180)/$pi;
    $lng = ($lng3*180)/$pi;
    return [$lat,$lng];
}