PHP algorithm calculates the distance between map coordinates of Baidu star small stack

Source: Internet
Author: User
Tags abs cos

/** calculate the distance between two coordinates (m) * @param float $fP 1Lat starting point (latitude) * @param float $fP 1Lon Start (longitude) * @param float $fP 2Lat end point (latitude) * @param Float $fP 2Lon end point (longitude) * @return int*/functionDistancebetween ($fP 1Lat,$fP 1Lon,$fP 2Lat,$fP 2Lon){    $fEARTH _radius= 6378137; //Angle converted to radians    $fRadLon 1=Deg2rad($fP 1Lon); $fRadLon 2=Deg2rad($fP 2Lon); $fRadLat 1=Deg2rad($fP 1Lat); $fRadLat 2=Deg2rad($fP 2Lat); //calculate the difference between latitude and longitude    $fD 1=ABS($fRadLat 1-$fRadLat 2); $fD 2=ABS($fRadLon 1-$fRadLon 2); //Distance Calculation    $fP=POW(Sin($fD 1/2), 2) +Cos($fRadLat 1) *Cos($fRadLat 2) *POW(Sin($fD 2/2), 2); return intval($fEARTH _radius* 2 *ASIN(sqrt($fP)) + 0.5);}/** * Baidu coordinate system converted to standard GPS sitting system * @param float $lnglat coordinates (such as: 106.426, 29.553404) * @return The standard GPS value after string conversion:*/functionBD09LLTOWGS84 ($lnglat){//Longitude, Latitude    $lnglat=Explode(‘,‘,$lnglat); List($x,$y) =$lnglat; $Baidu _server= "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={$x}&y={$y}"; $result= @file_get_contents($Baidu _server); $json= Json_decode ($result); if($json->error = = 0){        $BX=Base64_decode($json-x); $by=Base64_decode($json-y); $GPS _x= 2 *$x-$BX; $GPS _y= 2 *$y-$by; return $GPS _x.‘,‘.$GPS _y;//Longitude, Latitude}Else        return $lnglat;}

Cited by: http://www.jb51.net/article/59658.htm

PHP algorithm calculates the distance between map coordinates of Baidu star small stack

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.