Reputation: 356
May I know how to get current user location and calculate distance between location B in kilometer?
I tried below codes but seems like does not work.
<?php
echo "<script type = 'text/javascript'>
function showPosition(){
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
});
} else {
alert(\"Sorry, your browser does not support HTML5 geolocation.\");
}
}
</script>";
$point1 = array("lat" => $latitude, "long" => $longitude);
$point2 = array("lat" => $row_Merchant['latitude'], "long" => $row_Merchant['longitude']);
$km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); // Calculate distance in kilometres (default)
echo "$km km";
?>
<?php
function distanceCalculation($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2)
{
// Calculate the distance in degrees
$degrees = rad2deg(acos((sin(deg2rad($point1_lat)) * sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat)) * cos(deg2rad($point2_lat)) * cos(deg2rad($point1_long - $point2_long)))));
// Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles)
switch ($unit) {
case 'km':
$distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km)
break;
case 'mi':
$distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles)
break;
case 'nmi':
$distance = $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles)
}
return round($distance, $decimals);
}
?>
How can I pass in value latitude and longitude in $point1 = array("lat" => $latitude, "long" => $longitude); so that I can calculate distance between user location and lcoation B?
Please help. Thank you.
Upvotes: 0
Views: 389
Reputation: 2151
Eddy currently you're planning to assign javascript variable to PHP variable which is not the right way to do things as one in Client-side and other is Server-side. Reference.
I have changed your code to find distance between user's location and merchant's location.
<script type = 'text/javascript'>
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var radlon1 = Math.PI * lon1/180;
var radlon2 = Math.PI * lon2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344; }
if (unit=="N") { dist = dist * 0.8684; }
return dist;
}
function showPosition(dest_lon, dest_lat){
if(navigator.geolocation) {
console.log('hhhh');
navigator.geolocation.getCurrentPosition(function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log(latitude+','+longitude+','+ dest_lat+','+ dest_lon);
dist = distance(latitude, longitude, dest_lat, dest_lon, 'K');
console.log(dist); //final distance present in dist variable
});
} else {
alert("Sorry, your browser does not support HTML5 geolocation.");
}
}
var merch_longitude = 47.683277;
var merch_latitude = 3.210482;
showPosition(merch_longitude, merch_longitude);
</script>
In above code you'll pass database value to merch_longitude
and merch_latitude
currently it is hard coded. We will get current location of user using geolocation of Javascript, then showPosition()
will calculate distance.
Distance function takes 5 parameter lat1, long1, lat2, long2, unit.
By default unit is Miles, you can pass K for KM and N for NM.
Upvotes: 2