PHP calculates the distance of two points of geographic coordinates

Source: Internet
Author: User
Tags pow sin
Function: Calculates the spherical distance between two points according to the latitude and longitude of pi and earth radius coefficients and coordinates of two points.
Get two point coordinate distance:


    /** * Calculates the distance between two points of geographic coordinates * @param decimal $longitude 1 beginning Longitude * @param decimal $latitude 1 beginning latitude * @par Am Decimal $longitude 2 End Longitude * @param Decimal $latitude 2 End latitude * @param Int $unit Unit 1 : M 2: km * @param Int $decimal precision reserved decimal number * @return decimal * / functiongetdistance($longitude 1, $latitude 1, $ Longitude2, $latitude 2, $unit=2, $decimal=2){ $EARTH _radius=6370.996;//Earth radius coefficient$PI=3.1415926;$radLat 1=$latitude 1*$PI/180.0;$radLat 2=$latitude 2*$PI/180.0;$radLng 1=$longitude 1*$PI/180.0;$radLng 2=$longitude 2*$PI/180.0;$a=$radLat 1-$radLat 2;$b=$radLng 1-$radLng 2;$distance=2* ASIN (SQRT (Pow (sin ($a/2),2) + cos ($radLat 1) * COS ($radLat 2) * POW (sin ($b/2),2)));$distance=$distance*$EARTH _radius* +;if($unit==2){$distance=$distance/ +; }returnRound$distance,$decimal);}//Start coordinates$longitude 1=113.330405;$latitude 1=23.147255;//Terminal coordinates$longitude 2=113.314271;$latitude 2=23.1323;$distance= Getdistance ($longitude 1,$latitude 1,$longitude 2,$latitude 2,1);Echo$distance.' m ';//2342.38m$distance= Getdistance ($longitude 1,$latitude 1,$longitude 2,$latitude 2,2);Echo$distance.' km ';//2.34km?>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP two points of geographical coordinates of the distance, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.