Php calculates the distance between two points of geographic coordinates, and php two points of geographic coordinates
Function:The spherical distance between two points is calculated based on the circumference rate and the latitude and longitude of the earth's radius coefficient and the two-point coordinate.
Obtain the two-point coordinate distance:
<? Php/*** calculate the distance between two geographical coordinates * @ param Decimal $ longitude1 start longitude * @ param Decimal $ latitude1 start latitude * @ param Decimal $ longitude2 end longitude * @ param Decimal $ latitude2 end latitude * @ param Int $ unit 1: meters 2: Km * @ param Int $ decimal precision reserved Decimal places * @ return decimal */function getDistance ($ longitude1, $ latitude1, $ longitude2, $ latitude2, $ unit = 2, $ decimal = 2) {$ EARTH_RADIUS = 6370.996; // Earth radius coefficient $ PI = 3.1415926; $ radLat1 = $ lat Itude1 * $ PI/180.0; $ radLat2 = $ latitude2 * $ PI/180.0; $ radLng1 = $ longitude1 * $ PI/180.0; $ radLng2 = $ longitude2 * $ PI/180.0; $ a = $ radLat1-$ radLat2; $ B = $ radLng1-$ radLng2; $ distance = 2 * asin (sqrt (pow (sin ($ a/2), 2) + cos ($ radLat1) * cos ($ radLat2) * pow (sin ($ B/2), 2); $ distance = $ distance * $ EARTH_RADIUS * 1000; if ($ unit = 2) {$ distance = $ distance/1000;} return round ($ distance, $ Decimal);} // start coordinate $ longitude1 = 113.330405; $ latitude1 = 23.147255; // end coordinate $ longitude2 = 113.314271; $ latitude2 = 23.1323; $ distance = getDistance ($ longitude1, $ latitude1, $ longitude2, $ latitude2, 1); echo $ distance. 'M'; // 2342.38 m $ distance = getDistance ($ longitude1, $ latitude1, $ longitude2, $ latitude2, 2); echo $ distance. 'km '; // 2.34?>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.