2

I want to display distance between hotel and user. For calculation Distance between hotel and user address I have used following code. Distance value I have get on button click.

1) First I have getting use IP address and get all user address information from IP address.

$ipAddress  = $_SERVER['REMOTE_ADDR'];
$result = json_decode(file_get_contents("http://ip-api.com/json/{$ipAddress}"));

This call give me details like city, country, countryCode, latitude, longitude, zip etc. Save latitude and longitude.

$user_latitude = $result->lat;
$user_longitude = $result->lon;

2) I have hotel latitude and longitude in our database already.

now I have four value. User latitude/longitude and Hotel latitude/longitude
$lat1   =   $user_latitude;
$long1  =   $user_longitude;
$lat2   =   $hotel_latitude;
$long2  =   $hotel_longitude;

3) After that with the use of google map API I have get full address of both (User and Hotel)

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng={$lat1},{$long1}&sensor=true";
$url2 = "http://maps.googleapis.com/maps/api/geocode/json?latlng={$lat2},{$long2}&sensor=true";

# Get address of User
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$address1 = $response_a['results'][0]['formatted_address'];

# Get address of Hotel
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$address2 = $response_a['results'][0]['formatted_address'];

$find = array("'",'"');
$replace = array("","");
$address1 = str_replace($find,$replace,$address1);
$address2 = str_replace($find,$replace,$address2);

4) At last I have use distance matrix API of google and get distance between two points.

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&alternatives=true&mode=driving&language=de-DE&sensor=false";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);

$distance = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];

5) Distance is save in $distance and total time save in $time. It also give other details which you view in $response_a. Print the value of $response_a.

But all time it give me incorrect distance.

aidinMC
  • 1,415
  • 3
  • 18
  • 35
Vimal Patel
  • 73
  • 1
  • 3
  • 5
  • 2
    Once you have two latitudes/longitudes, you simply use the *Great Circle distance formula*; it's pretty simple, look it up. No need to convert coordinates to addresses and use APIs. Having said this, yes, looking up a location *by IP* will only give you very very ***veeeery*** rough locations at best and accuracy will only be good up to city or state level; most certainly not accurate to within a few kilometers. – deceze Aug 15 '16 at 07:05

2 Answers2

0

This stackoverflow thread has several implementations of the Haversine formula, which is used for distance calculation. In that thread, a PHP implementation is included as well, which I blatantly copied and pasted below.

<?php function distance($lat1, $lon1, $lat2, $lon2) {

    $pi80 = M_PI / 180;
    $lat1 *= $pi80;
    $lon1 *= $pi80;
    $lat2 *= $pi80;
    $lon2 *= $pi80;

    $r = 6372.797; // mean radius of Earth in km
    $dlat = $lat2 - $lat1;
    $dlon = $lon2 - $lon1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;

    //echo '<br/>'.$km;
    return $km;
}
?>  

All credits go to the guy who posted the implementation in the thread linked above.

Community
  • 1
  • 1
derjoachim
  • 272
  • 1
  • 2
  • 7
  • 1
    Thank you for answered I want calculate distance by road. I have already use function which you give. but it also give me wrong distance. – Vimal Patel Aug 15 '16 at 09:08
0

If you want the distance between two point, you could use that, but if you want an itinerary's distance, you might want to use Google Directions API or some equivalent.

Community
  • 1
  • 1
vincenth
  • 1,732
  • 5
  • 19
  • 27
  • Thank you for your answere I have already use distance matrix API of google map – Vimal Patel Aug 15 '16 at 09:11
  • @vincenth It is not accurate, it returns rough results with more then 50 km difference from real location. For your information i am working on same project as Vimal Patel – papacico Aug 15 '16 at 21:21
  • Then you could try other services, I think OpenStreetMap provides an equivalent, but I can't help you further. Good luck. – vincenth Aug 15 '16 at 21:44