php mysql calculate distance between 2 points -
i have map page contains google map , form calculate distance between 2 selected points database, don't know how calculate distance between 2 locations using php or ajax , jquery.
i have village table contains:
- id
- village_name
- latitude
- longitude
this have in code far:
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 * 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 location 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>
assuming formular looks this:
<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>
your calculate_distance.php
script similar answer provided here: calc distance of 2 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; ?> ...
Comments
Post a Comment