- <?php
- Define (' Earth_radius ', 6378.137);//Earth radius
- Define (' PI ', 3.1415926);
- /**
- * Calculate the distance between the two sets of latitude coordinates
- * PARAMS:LAT1 latitude 1; lng1 longitude 1; lat2 latitude 2; lng2 longitude 2; Len_type (1:m or 2:km);
- * Return m or km
- */
- function Getdistance ($lat 1, $lng 1, $lat 2, $lng 2, $len _type = 1, $decimal = 2)
- {
- $radLat 1 = $lat 1 * pi/180.0;
- $radLat 2 = $lat 2 * pi/180.0;
- $a = $radLat 1-$radLat 2;
- $b = ($lng 1 * pi/180.0)-($lng 2 * pi/180.0);
- $s = 2 * ASIN (SQRT (POW) (sin ($a/2), 2) + cos ($radLat 1) * cos ($radLat 2) * POW (sin ($b/2), 2));
- $s = $s * earth_radius;
- $s = round ($s * 1000);
- if ($len _type > 1)
- {
- $s/= 1000;
- }
- Return round ($s, $decimal);
- }
- Echo getdistance (39.908156,116.4767, 39.908452, 116.450479, 1);//output distance/m
This article links http://www.cxybl.com/html/wlbc/Php/20130319/37247.html