PHP calculates the distance between two coordinates (longitude, latitude), coordinates longitude _php tutorial

Source: Internet
Author: User
Tags cos

PHP calculates the distance between two coordinates (longitude, latitude), coordinates longitude


This example describes how PHP calculates the distance between two coordinates (longitude, latitude). Share to everyone for your reference. Specific as follows:

Here we use PHP to calculate the distance between two coordinates (longitude, latitude) and return the result to meters or kilometers.

function distance ($lat 1, $lng 1, $lat 2, $lng 2, $miles = True) {$pi = m_pi/180; $lat 1 *= $pi; $lng 1 *= $pi; $lat 2 *= $PI 80; $LNG 2 *= $PI 80; $r = 6372.797; Mean radius of Earth in miles $dlat = $lat 2-$lat 1; $DLNG = $lng 2-$LNG 1; $a = sin ($dlat/2) *sin ($dlat/2) +cos ($lat 1) *cos ($lat 2) *sin ($DLNG/2) *sin ($DLNG/2); $c = 2 * atan2 (sqrt ($a), sqrt (1-$a)); $km = $r * $c; Return ($miles? ($KM * 0.621371192): $km);}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/985268.html www.bkjia.com true http://www.bkjia.com/PHPjc/985268.html techarticle PHP calculates the distance between two coordinates (longitude, latitude), coordinate longitude This example describes the method by which PHP calculates the distance between two coordinates (longitude, latitude). To share with you ...

  • Related Article

    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.