PHP calculates the distance between two specified longitude and latitude locations, which are used to compute the latitude and longitude of a given location, calculate the business area near it, and the distance between a given location and a nearby business district. The following are the specific function codes and usage examples.
about how to obtain the latitude and longitude of an address, can refer to this site article:
Google Map Third edition based on geographical location to obtain latitude and longitude method
<?php
/**
* Find the distance between two known latitude and longitude, Unit m
* @param lng1,lng2 Longitude
* @param lat1,lat2 latitude
* @return float distance, Unit m
* @author www. alixixi.com
**/
function getdistance ($lng 1, $lat 1, $lng 2, $lat 2) {
//convert angle to Fox degree
$radLat 1=deg2rad ($ LAT1); the//deg2rad () function converts the angle to radians
$radLat 2=deg2rad ($lat 2);
$radLng 1=deg2rad ($lng 1);
$radLng 2=deg2rad ($lng 2);
$a = $radLat 1-$radLat 2;
$b = $radLng 1-$radLng 2;
$s =2*asin (sqrt pow (sin ($a/2), 2) +cos ($radLat 1) *cos ($radLat 2) *pow (sin ($b/2), 2)) *6378.137*1000;
return $s;
}
Here to calculate the "Shanghai Yan ' an west road 2055 Lane" to "Shanghai Jingansi" Distance:
2055, Yanan West Road, Shanghai, China latitude and longitude degree: 31.2014966,121.40233369999998
Shanghai Jingansi Warp Latitude: 31.22323799999999,121.44552099999998
The
echo getdistance (31.2014966,121.40233369999998,31.22323799999999,121.44552099999998);
The result is: 4970.4248747365, about 4970 meters, about 10 miles, I used to travel back and forth in this 2.1-line experience, should be more or less.