一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
Swift-MapNavigation(经纬度转换)代码下载
文件
代码使用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - MapNavigation.swift
//GCJ-02(高德)坐标 转 BD-09(百度)坐标
let mapNav = MapNavigation()
// 经纬度格式为 "纬度,经度"
mapNav.poGaodeScanner(endLocation: "39.9042,116.4074") // 北京坐标示例
// MARK: - MapTransformLocation.swift
// 初始化坐标转换工具
let converter = MapTransformLocation()
// 定义测试坐标 (latitude, longitude)
let originalCoordinate = CLLocationCoordinate2D(latitude: 39.9042, longitude: 116.4074) // Beijing coordinates
// 1. WGS-84(全球通用坐标) → GCJ-02(高德坐标)
let gcj02Coord = converter.wgs84ToGcj02(location: originalCoordinate)
print("GCJ-02 Coordinate: \(gcj02Coord.latitude), \(gcj02Coord.longitude)")
// 2. GCJ-02(高德坐标) → WGS-84(全球通用坐标 -- 有1-2米误差)
let wgs84Coord = converter.gcj02ToWgs84(location: gcj02Coord)
print("WGS-84 Coordinate: \(wgs84Coord.latitude), \(wgs84Coord.longitude)")
// 3. WGS-84(全球通用坐标 -- 有1-2米误差) → BD-09(百度坐标)
let bd09Coord = converter.wgs84ToBd09(location: originalCoordinate)
print("BD-09 Coordinate: \(bd09Coord.latitude), \(bd09Coord.longitude)")
// 4. GCJ-02(高德坐标) → BD-09(百度坐标)
let gcjToBd09 = converter.gcj02ToBd09(location: gcj02Coord)
print("GCJ-02 to BD-09: \(gcjToBd09.latitude), \(gcjToBd09.longitude)")
// 5. BD-09(百度坐标) → GCJ-02(高德坐标)
let bdToGcj02 = converter.bd09ToGcj02(location: bd09Coord)
print("BD-09 to GCJ-02: \(bdToGcj02.latitude), \(bdToGcj02.longitude)")
// 6. BD-09(百度坐标) → WGS-84(全球通用坐标 -- 有1-2米误差)
let bdToWGS84 = converter.bd09ToWgs84(location: bd09Coord)
print("BD-09 to WGS-84: \(bdToGcj02.latitude), \(bdToGcj02.longitude)")
}
}