How can calculate distance between two place in php with google map?

<?php

$q = “http://maps.googleapis.com/maps/api/distancematrix/json?origins=&#8221;.str_replace(‘ ‘,’+’,$_REQUEST[‘address1’]).”&destinations=”.str_replace(‘ ‘,’+’,$_REQUEST[‘address2’]).”&mode=driving&sensor=false”;

$json = file_get_contents($q);

$details = json_decode($json, TRUE);

//echo “<pre>”; print_r($details); echo “</pre>”;

$tim = explode(‘ ‘,$details[‘rows’][0][‘elements’][0][‘duration’][‘text’]);

//print $tim[0]=60;
switch ($tim[1]):
case ‘day’:
$time = $tim[0]*24*60;
break;
case ‘days’:
$time = $tim[0]*24*60;
break;
case ‘hour’:
$time =  $tim[0]*60;
break;
case ‘hours’:
$time =  $tim[0]*60;
break;
default:
$time = $tim[0];
endswitch;

if($tim[3]==”hours”)
{
$time = $time+($tim[2]*60);
}
if($tim[3]==”mins”)
{
$time = $time+$tim[2];
}

//print ‘<br>ftim=’.$time;

$dis = explode(‘ ‘,$details[‘rows’][0][‘elements’][0][‘distance’][‘text’]);
//print_r($details[‘rows’][0][‘elements’][0][‘distance’][‘text’]);

$distance = str_replace(“,”, “”, $dis[0]) *0.621371192;

?>

body tag code

<script type=”text/javascript”>
var map;
var geocoder, location1, location2;
function initialize()
{
geocoder = new GClientGeocoder();
}
function showmap()
{

var add1='<?php echo $_REQUEST[‘address1′];?>’;

geocoder.getLocations(add1, function (response)
{

if (!response || response.Status.code != 200)
{
alert(“Sorry, we were unable to find the first address”);
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
calculate();
}
});
}

function calculate()
{

var lt=<?php echo $latitude;?>;
var ll=<?php echo $longitude;?>;

if (GBrowserIsCompatible())
{
// create map and add controls
var map = new GMap2(document.getElementById(“map”));
map.addControl(new GLargeMapControl());

// set centre point of map
var centrePoint = new GLatLng(lt, ll);
map.setCenter(centrePoint, 8);

// add a draggable marker
var marker = new GMarker(centrePoint, {draggable: true});
map.addOverlay(marker);

// add a drag listener to the map
GEvent.addListener(marker, “dragend”, function() {
var point = marker.getPoint();
map.panTo(point);
document.getElementById(“latitude”).value = point.lat();
document.getElementById(“longitude”).value = point.lng();
});
}

}

function showLocation() {

var add1='<?php echo $_REQUEST[‘address1′];?>’;
var add2='<?php echo $_REQUEST[‘address2′];?>’;

geocoder.getLocations(add1, function (response) {

if (!response || response.Status.code != 200)
{
alert(“Sorry, we were unable to find the first address”);
}
else
{
location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
geocoder.getLocations(add2, function (response) {
if (!response || response.Status.code != 200)
{
alert(“Sorry, we were unable to find the second address”);
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
calculateDistance();
}
});
}
});
}

function calculateDistance()
{
var lt=<?php print $latitude;?>;
var ll=<?php print $longitude; ?>;
var rt=<?php print $latitude1;?>;
var rl=<?php print $longitude1; ?>;
try
{
var glatlng1 = new GLatLng(<?php print $latitude;?>, <?php print $longitude;?>);
var glatlng2 = new GLatLng(<?php print $latitude1;?>, <?php print $longitude1;?>);
var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
var kmdistance = (miledistance * 1.609344).toFixed(1);
var time=(kmdistance/30).toFixed(2);
var t=time %1;
if (t > .60)
{

var time=((time-t)+1).toFixed(2);
}

/*document.getElementById(‘values’).innerHTML = ‘<strong>Address 1: </strong><br/>’ + ‘<?php //print $_REQUEST[‘address1′]?>’ + ‘<br /><strong>Address 2: </strong><br/>’ + ‘<?php //print $_REQUEST[‘address2′]?>’ + ‘<br /><strong>Distance: </strong><br/>’ + miledistance + ‘ Miles’ + ‘<br />’+kmdistance + ‘ KiloMeters’+'<br /><strong>Travelling Time:</strong><br />’+time+ ‘Hours’;*/
}
catch (error)
{
alert(error);
}
if (GBrowserIsCompatible())
{
// create map and add controls
var map = new GMap2(document.getElementById(“map”));
map.addControl(new GLargeMapControl());

// set centre point of map
var centrePoint = new GLatLng(lt, ll);
map.setCenter(centrePoint, 8);

var centre = new GMarker(new GLatLng(rt, rl));
map.addOverlay(centre, 8);

var points = [new GLatLng(lt, ll),new GLatLng(rt, rl)];
var polyline = new GPolyline(points, ‘#f00’, 6);

map.addOverlay(polyline);

// add a draggable marker
var marker = new GMarker(centrePoint, {draggable: true});
map.addOverlay(marker);

// add a drag listener to the map
GEvent.addListener(marker, “dragend”, function() {
var point = marker.getPoint();
map.panTo(point);
document.getElementById(“latitude”).value = point.lat();
document.getElementById(“longitude”).value = point.lng();
});
}

}

</script>
<script>initialize();</script>
<script>

window.onload = function load() {

showmap();
showLocation();
}

//]]>
</script>

in div

<p>Estimated distance <strong><?php print ceil($distance)?> miles</strong>, estimated time <strong><?php print ceil($time);?> minutes</strong>.</p>

div code

<div align=”center” id=”map” style=”width: 642px; height: 350px”></div>

Advertisements

2 thoughts on “How can calculate distance between two place in php with google map?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s