[英]How to calculate distance between two places using php?
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. 1)首先我要使用IP地址,并从IP地址获取所有用户地址信息。
$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. 2)我已经在我们的数据库中找到了酒店的经度和纬度。
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) 3)之后,通过使用谷歌地图API,我获得了(用户和酒店)的完整地址
$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. 4)最后,我使用了谷歌的距离矩阵API,并得到两点之间的距离。
$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. 5)距离以
$distance
保存,总时间以$ time保存。 It also give other details which you view in $response_a
. 它还提供了您在
$response_a
查看的其他详细信息。 Print the value of $response_a
. 打印
$response_a
的值。
But all time it give me incorrect distance. 但是所有的时间都给我不正确的距离。
This stackoverflow thread has several implementations of the Haversine formula , which is used for distance calculation. 该stackoverflow线程具有Haversine公式的几种实现,该公式用于距离计算。 In that thread, a PHP implementation is included as well, which I blatantly copied and pasted below.
在该线程中,还包括一个PHP实现,我在下面对其进行了公然复制和粘贴。
<?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. 所有功劳归功于在上面链接的线程中发布实现的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.