Skip to content

Commit

Permalink
fix earth_r constants for swift
Browse files Browse the repository at this point in the history
  • Loading branch information
aokizen committed Jun 11, 2016
1 parent 4b10b87 commit f8788e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions swift/LocationTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Foundation
* Struct transform coordinate between earth(WGS-84) and mars in china(GCJ-02).
*/
public struct LocationTransform {

static let EARTH_R: Double = 6378137.0

static func isOutOfChina(lat: Double, lng: Double) -> Bool {

Expand Down Expand Up @@ -43,15 +45,14 @@ public struct LocationTransform {
}

static func delta(lat: Double, lng: Double) -> (dLat: Double, dLng: Double) {
let r = 6378137.0
let ee = 0.00669342162296594323
let radLat = lat / 180.0 * M_PI
var magic = sin(radLat)
magic = 1 - ee * magic * magic
let sqrtMagic = sqrt(magic)
var (dLat, dLng) = transform(lng - 105.0, y: lat - 35.0)
dLat = (dLat * 180.0) / ((r * (1 - ee)) / (magic * sqrtMagic) * M_PI)
dLng = (dLng * 180.0) / (r / sqrtMagic * cos(radLat) * M_PI)
dLat = (dLat * 180.0) / ((EARTH_R * (1 - ee)) / (magic * sqrtMagic) * M_PI)
dLng = (dLng * 180.0) / (EARTH_R / sqrtMagic * cos(radLat) * M_PI)
return (dLat, dLng)
}

Expand Down Expand Up @@ -113,7 +114,6 @@ public struct LocationTransform {
* Distance calculate the distance between point(latA, lngA) and point(latB, lngB), unit in meter.
*/
public static func Distance(latA: Double, lngA: Double, latB: Double, lngB: Double) -> Double {
let earthR = 6371000.0
let arcLatA = latA * M_PI / 180
let arcLatB = latB * M_PI / 180
let x = cos(arcLatA) * cos(arcLatB) * cos((lngA-lngB) * M_PI/180)
Expand All @@ -126,7 +126,7 @@ public struct LocationTransform {
s = -1
}
let alpha = acos(s)
let distance = alpha * earthR
let distance = alpha * EARTH_R
return distance
}
}
Expand Down
10 changes: 5 additions & 5 deletions swift/test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class EvilTransformTests: XCTestCase {
func testGcj2wgs() {
for (wgsLat, wgsLng, gcjLat, gcjLng) in TESTS {
let ret = LocationTransform.gcj2wgs(gcjLat, gcjLng: gcjLng)
XCTAssertTrue(fabs(ret.wgsLat - wgsLat) <= lowAccuracy)
XCTAssertTrue(fabs(ret.wgsLng - wgsLng) <= lowAccuracy)
XCTAssertTrue(fabs(ret.wgsLat - wgsLat) < lowAccuracy)
XCTAssertTrue(fabs(ret.wgsLng - wgsLng) < lowAccuracy)
}
}

Expand Down Expand Up @@ -77,8 +77,8 @@ class EvilTransformTests: XCTestCase {
func testWgs2bd() {
for (bdLat, bdLng, wgsLat, wgsLng) in TESTS_bd {
let ret = LocationTransform.wgs2bd(wgsLat, wgsLng: wgsLng)
XCTAssertTrue(fabs(ret.bdLat - bdLat) <= lowAccuracy)
XCTAssertTrue(fabs(ret.bdLng - bdLng) <= lowAccuracy)
XCTAssertTrue(fabs(ret.bdLat - bdLat) < lowAccuracy)
XCTAssertTrue(fabs(ret.bdLng - bdLng) < lowAccuracy)
}
}

Expand All @@ -103,4 +103,4 @@ class EvilTransformTests: XCTestCase {
LocationTransform.bd2wgs(bdLat, bdLng: bdLng)
}
}
}
}

0 comments on commit f8788e5

Please sign in to comment.