PHPz2017-04-17 17:30:05
The Java version I used on Android before has been converted to Swift for you. After running it, there should be no problem.
First use the first function to convert Baidu coordinates into Mars coordinates, and then use the second function to convert Mars coordinates into WGS84 coordinates, which are the real coordinates.
let PI = 3.1415926535897932384626;
let A = 6378245.0;
let EE = 0.00669342162296594323;
//: Change BD-09 to Gcj-02
func bd09_to_gcj02(latitude lat:Double, longitude lon:Double) -> (lat:Double, lon:Double)
{
let x: Double = lon - 0.0065;
let y: Double = lat - 0.006;
let z: Double = sqrt(x * x + y * y) - 0.00002 * sin(y * PI);
let theta: Double = atan2(y, x) - 0.000003 * cos(x * PI);
return (z * sin(theta), z * cos(theta));
}
//: Change Gcj-02 to WGS-84
func gcj02_to_wgs84(latitude lat:Double, longitude lon:Double) -> (lat:Double, lon:Double)
{
let wgs84_pos: (lat: Double, lon: Double) = tran(latitude: lat, longitude: lon);
let gcj02_lon: Double = lon * 2 - wgs84_pos.lon;
let gcj02_lat: Double = lat * 2 - wgs84_pos.lat;
return (gcj02_lat, gcj02_lon);
}
//: Private func
private func tran(latitude lat:Double, longitude lon:Double) -> (lat:Double, lon:Double)
{
var dLat: Double = transformLat(lon - 105.0, y: lat - 35.0);
var dLon: Double = transformLon(lon - 105.0, y: lat - 35.0);
let radLat: Double = lat / 180.0 * PI;
var magic: Double = sin(radLat);
magic = 1 - EE * magic * magic;
let sqrtMagic: Double = sqrt(magic);
dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (A / sqrtMagic * cos(radLat) * PI);
let mgLat: Double = lat + dLat;
let mgLon: Double = lon + dLon;
return (mgLat, mgLon);
}
private func transformLat(x: Double, y: Double) -> Double
{
var ret: Double = -100.0 + 2.0 * x + 3.0 * y;
ret += 0.2 * y * y + 0.1 * x * y;
ret += 0.2 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * PI) + 20.0 * sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * PI) + 40.0 * sin(y / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * PI) + 320 * sin(y * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
private func transformLon(x: Double, y: Double) -> Double
{
var ret: Double = 300.0 + x + 2.0 * y;
ret += 0.1 * x * x + 0.1 * x * y;
ret += 0.1 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * PI) + 20.0 * sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * PI) + 40.0 * sin(x / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * PI) + 300.0 * sin(x / 30.0 * PI)) * 2.0 / 3.0;
return ret;
}
黄舟2017-04-17 17:30:05
During the front-end time, I wrote a js version of commonly used Internet coordinate conversion, which exactly corresponds to your problem. In addition, the GPS coordinates are WGS84 coordinates, http://wandergis.github.io/coordtransform/ github
今天挂了,只能给你这个了,你可以使用npm下载使用,还提供了一个python
version of your You can go take a look. I just found out that you asked the question under ios. The source code is open source. You can refer to the code modified into objectivec or swift
PHPz2017-04-17 17:30:05
Baidu coordinates seem to be modified once again from Mars coordinates. There are Baidu coordinates converted to Mars coordinates, and Mars coordinates converted to Earth coordinates. You can find these on github or Baidu.
By the way, you have already used Baidu Maps, why do you still need to know the real earth coordinates.
大家讲道理2017-04-17 17:30:05
Baidu Map has an interface that can be converted. You can check the API of Baidu Map