php mysql计算2点之间的距离

时间:2013-05-04 14:12:13

标签: php mysql jquery google-maps-api-3

我有一个地图页面,其中包含一张Google地图和一张表格,用于计算数据库中两个选定点之间的距离,但我不知道如何使用PHP或AJAX和jQuery计算两个位置之间的距离。< / p>

我有一张包含以下内容的乡村表:

  • ID
  • village_name
  • 纬度
  • 经度

到目前为止,这是我的代码中的内容:

map.php

<?php require_once('include/connect.php'); ?>
<?php
session_start();

   if(isset($_SESSION['login']) != 'true'){
        header("location: index.php");

        var_dump( $_SESSION['login']);
    }


   $login = ($_SESSION['login']);
   $userid = ($_SESSION['user_id']);
   $login_user = ($_SESSION['username']);
   $fname = ($_SESSION['first_name']);
   $lname = ($_SESSION['last_name']);
?>
<?php require_once('header.php'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Lam_El_Chamel</title>
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Abel|Satisfy' rel='stylesheet' type='text/css' />
<link href="default.css" rel="stylesheet" type="text/css" media="all" />
<!--[if IE 6]>
<link href="default_ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->

<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

 <style type="text/css">
            body { font: normal 10pt Helvetica, Arial; }
            #map { width: 850px; height: 500px; border: 0px; padding: 0px;left:0px;;}
            #calculate-distance-form{position:absolute; top:600px; right:-120px; width:570px; height:200;}
        </style>
        <script src="http://maps.google.com/maps/api/js?key=AIzaSyAcdPXDgq5V8wmTiJedTBXGqIhzq8XdKdc&sensor=false" type="text/javascript"></script>
<script type="text/javascript">
      var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
                       new google.maps.Size(32, 32), new google.maps.Point(0, 0),
                       new google.maps.Point(16, 32));
            var center = null;
            var map = null;
            var currentPopup;
            var bounds = new google.maps.LatLngBounds();
            function addMarker(lat, lng, info) {
                var pt = new google.maps.LatLng(lat, lng);
                bounds.extend(pt);
                var marker = new google.maps.Marker({
                    position: pt,
                    icon: icon,
                    map: map
                });
                var popup = new google.maps.InfoWindow({
                    content: info,
                    maxWidth: 300
                });
                google.maps.event.addListener(marker, "click", function() {
                    if (currentPopup != null) {
                        currentPopup.close();
                        currentPopup = null;
                    }
                    popup.open(map, marker);
                    currentPopup = popup;
                });
                google.maps.event.addListener(popup, "closeclick", function() {
                    map.panTo(center);
                    currentPopup = null;
                });
            }           
            function initMap() {
                map = new google.maps.Map(document.getElementById("map"), {
                    center: new google.maps.LatLng(0, 0),
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    mapTypeControl: true,
                    mapTypeControlOptions: {
                        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
                    },
                    navigationControl: true,
                    navigationControlOptions: {
                        style: google.maps.NavigationControlStyle.ZOOM_PAN
                    }
                });


<?php

$query = mysql_query("SELECT * FROM location")or die(mysql_error());
while($row = mysql_fetch_array($query))
{
  $name = $row['user_name'];
  $lat = $row['lattitude'];
  $lon = $row['longitude'];
  //$desc = $row['desc'];



  echo("addMarker($lat, $lon, '<b>$name</b><br />');\n");

}

?>
 center = bounds.getCenter();
     map.fitBounds(bounds);

     }
     </script>
     <script type="text/javascript">
        $(document).ready(function(){
            $("select#location2").attr("disabled","disabled");
             $("select#location1").change(function(){
              id = $(this).val();
            $("select#location2").attr("disabled","disabled");
            $("select#location2").html("<option>wait...</option>");
            $.post("select_location.php", {id:id}, function(data){
            $("select#location2").removeAttr("disabled");
            $("select#location2").html(data);
         });
        });
      });
     </script>

</head>
<body onload="initMap()" style="margin:0px; border:0px; padding:0px;">



<div id="wrapper">
    <div id="page-wrapper">
        <div id="page">
            <div id="wide-content">
                <h2>Map Border .....See Your Location with Google Map</h2> 
                <div id="map"></div>
                <div id="calculate-distance-form">
                <?php require_once('include/select.class.php'); ?>
                   <form action="calculate_distance.php" method="post">
                   Location one:
             <select id="location1" name="pt1">
                         <?php echo $opt->Showlocation() ?>
                      </select><br />
                               <br />

              Location two:
             <select id="location2" name="pt2">
                          <option value="0">choose...</option>
                      </select><br />
                               <br />

                      <input type="submit" name="calculate" value="Calculate Distance" />
                </form>
                   <!--
                   First Location<br />
                   <input type="text" name="lat1" placeholder="first Latitude" />
                   <input type="text" name="lon1" placeholder="first longitude"/><br />
                   Second Location<br />
                   <input type="text" name="lat2" placeholder="second latitude" />
                   <input type="text" name="lon2" placeholder="second longitude"/><br />
                   <input type="submit" name="calculate-distance" value="Calculate Distance" />
                  --> 
               </div>   
            </div>
        </div>
  </div>
</div>
<?php require_once('footer.php'); ?>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

假设您的公式如下:

<form action="calculate_distance.php" method="post">

    <input type="text" name="lat1" placeholder="first Latitude" />
    <input type="text" name="lng1" placeholder="first longitude"/><br />

    <input type="text" name="lat2" placeholder="second latitude" />
    <input type="text" name="lng2" placeholder="second longitude"/><br />     

    <input type="submit" name="calculate" value="Calculate Distance" />
</form>

您的calculate_distance.php脚本可能类似于此处提供的答案:Calc distance of two points in PHP

...
<?php
    $pi80 = M_PI / 180;
    $lat1 = floatval($_POST['lat1']) * $pi80;
    $lng1 = floatval($_POST['lng1']) * $pi80;
    $lat2 = floatval($_POST['lat2']) * $pi80;
    $lng2 = floatval($_POST['lng2']) * $pi80;

    $r = 6372.797; // earth radius
    $dlat = $lat2 - $lat1;
    $dlng = $lng2 - $lng1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;

    echo "Distance: ". $km;
?>
...