網路上有找到Google的計算公式,把它翻譯成Swift的語法後如下,回傳單位為**公里 **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static func GetDistance_Google(pointA:CLLocationCoordinate2D , pointB:CLLocationCoordinate2D) -> Double { let EARTH_RADIUS:Double = 6378.137;
let radlng1:Double = pointA.longitude * M_PI / 180.0; let radlng2:Double = pointB.longitude * M_PI / 180.0;
let a:Double = radlng1 - radlng2; let b:Double = (pointA.latitude - pointB.latitude) * M_PI / 180; var s:Double = 2 * asin(sqrt(pow(sin(a/2), 2) + cos(radlng1) * cos(radlng2) * pow(sin(b/2), 2)));
s = s * EARTH_RADIUS; s = (round(s * 10000) / 10000); return s; }
|