Php calculates the distance between two points of geographic coordinates, and php two points of geographic coordinates

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.