iOS 跳轉第三方地圖
阿新 • • 發佈:2018-11-08
1.配置info.plist
2. 程式碼, 跳轉第三方地圖路徑規劃
//跳轉到第三方地圖 APP protocol NaviActionProtocol { func toMapApps(vc: UIViewController, latitude: Double?, longitude: Double?, destName: String?) } //擴充套件實現協議方法 extension NaviActionProtocol { func toMapApps(vc: UIViewController, latitude: Double?, longitude: Double?, destName: String?) { guard let lati = latitude, let longi = longitude else { MBProgressHUD.showText("未獲取到目的地經緯度", toView: nil) return } let destinationName = destName ?? "" let actionSheet = UIAlertController(title: "請選擇地圖", message: nil, preferredStyle: .actionSheet) //蘋果自帶高德地圖 let appleAction = UIAlertAction(title: "蘋果地圖", style: .default) { (action) in let loc = CLLocationCoordinate2DMake(latitude!, longitude!) let currentLocation = MKMapItem.forCurrentLocation() let toLocation = MKMapItem.init(placemark: MKPlacemark.init(coordinate: loc, addressDictionary: nil)) //傳入終點名稱 toLocation.name = destinationName MKMapItem.openMaps(with: [currentLocation, toLocation], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: NSNumber.init(booleanLiteral: true)]) } actionSheet.addAction(appleAction) //utf-8編碼 var charSet = CharacterSet.urlQueryAllowed charSet.insert(charactersIn: "#") //騰訊地圖 if UIApplication.shared.canOpenURL(URL(string: "qqmap://")!) { let tencentAction = UIAlertAction(title: "騰訊地圖", style: .default) { (action) in let tecentStr = String(format: "qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=%@&coord_type=&policy=0", lati, longi, destinationName) if #available(iOS 10.0, *) { UIApplication.shared.open(URL(string: tecentStr.addingPercentEncoding(withAllowedCharacters: charSet)!)!, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(URL(string: tecentStr.addingPercentEncoding(withAllowedCharacters: charSet)!)!) } } actionSheet.addAction(tencentAction) } //高德地圖 if UIApplication.shared.canOpenURL(URL(string: "iosamap://")!) { let gaodeAction = UIAlertAction(title: "高德地圖", style: .default) { (action) in let urlStr = String(format: "iosamap://path?sourceApplication= &sid=&dlat=%f&dlon=%f&dname=%@", lati, longi, destinationName) if #available(iOS 10.0, *) { UIApplication.shared.open(URL(string: urlStr.addingPercentEncoding(withAllowedCharacters: charSet)!)!, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(URL(string: urlStr.addingPercentEncoding(withAllowedCharacters: charSet)!)!) } } actionSheet.addAction(gaodeAction) } //百度地圖 if UIApplication.shared.canOpenURL(URL(string: "baidumap://")!) { let baiduAction = UIAlertAction(title: "百度地圖", style: .default) { (action) in let urlStr = String(format: "baidumap://map/direction?origin={{我的位置}}&destination=name:%@|latlng:%f,%f&mode=driving&coord_type=gcj02", destinationName, lati, longi) if #available(iOS 10.0, *) { UIApplication.shared.open(URL(string: urlStr.addingPercentEncoding(withAllowedCharacters: charSet)!)!, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(URL(string: urlStr.addingPercentEncoding(withAllowedCharacters: charSet)!)!) } } actionSheet.addAction(baiduAction) } let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil) actionSheet.addAction(cancelAction) vc.present(actionSheet, animated: true, completion: nil) } }
注意事項:
1. 程式碼實現的是跳轉第三方路徑規劃功能, 要直接跳轉導航介面, 請參考各自官方文件, 修改 URL 中相應欄位
高德 URL 欄位說明: https://lbs.amap.com/api/amap-mobile/guide/ios/ios-uri-information
騰訊 URL 欄位說明: https://lbs.qq.com/uri_v1/index.html
百度 URL 欄位說明: https://lbsyun.baidu.com/index.php?title=uri/api/ios
2.跳轉蘋果自帶地圖, 在駕車,步行,公交來回切換, 蘋果地圖會出現閃退
3.注意座標轉換, 後臺資料座標型別一般為 GPS 座標, 高德地圖使用的是火星座標(GCJ02), 騰訊地圖預設使用的也是火星座標, 百度地圖預設使用的是百度座標.騰訊地圖和百度地圖都可以通過修改 URL 中 coord_type欄位修改座標型別.
座標轉換工具類:
OC:
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface APLocationConverter : NSObject /** * @brief 世界標準地理座標(WGS-84) 轉換成 中國國測局地理座標(GCJ-02)<火星座標> * * ####只在中國大陸的範圍的座標有效,以外直接返回世界標準座標 * * @param location 世界標準地理座標(WGS-84) * * @return 中國國測局地理座標(GCJ-02)<火星座標> */ + (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location; /** * @brief 中國國測局地理座標(GCJ-02) 轉換成 世界標準地理座標(WGS-84) * * ####此介面有1-2米左右的誤差,需要精確定位情景慎用 * * @param location 中國國測局地理座標(GCJ-02) * * @return 世界標準地理座標(WGS-84) */ + (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location; /** * @brief 世界標準地理座標(WGS-84) 轉換成 百度地理座標(BD-09) * * @param location 世界標準地理座標(WGS-84) * * @return 百度地理座標(BD-09) */ + (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location; /** * @brief 中國國測局地理座標(GCJ-02)<火星座標> 轉換成 百度地理座標(BD-09) * * @param location 中國國測局地理座標(GCJ-02)<火星座標> * * @return 百度地理座標(BD-09) */ + (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location; /** * @brief 百度地理座標(BD-09) 轉換成 中國國測局地理座標(GCJ-02)<火星座標> * * @param location 百度地理座標(BD-09) * * @return 中國國測局地理座標(GCJ-02)<火星座標> */ + (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location; /** * @brief 百度地理座標(BD-09) 轉換成 世界標準地理座標(WGS-84) * * ####此介面有1-2米左右的誤差,需要精確定位情景慎用 * * @param location 百度地理座標(BD-09) * * @return 世界標準地理座標(WGS-84) */ + (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location; @end
#import "APLocationConverter.h"
#import <CoreLocation/CoreLocation.h>
#define LAT_OFFSET_0(x,y) -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x))
#define LAT_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_2 (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0
#define LON_OFFSET_0(x,y) 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x))
#define LON_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_2 (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_3 (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0
#define RANGE_LON_MAX 137.8347
#define RANGE_LON_MIN 72.004
#define RANGE_LAT_MAX 55.8271
#define RANGE_LAT_MIN 0.8293
// jzA = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
#define jzA 6378245.0
#define jzEE 0.00669342162296594323
@implementation APLocationConverter
+ (double)transformLat:(double)x bdLon:(double)y
{
double ret = LAT_OFFSET_0(x, y);
ret += LAT_OFFSET_1;
ret += LAT_OFFSET_2;
ret += LAT_OFFSET_3;
return ret;
}
+ (double)transformLon:(double)x bdLon:(double)y
{
double ret = LON_OFFSET_0(x, y);
ret += LON_OFFSET_1;
ret += LON_OFFSET_2;
ret += LON_OFFSET_3;
return ret;
}
+ (BOOL)outOfChina:(double)lat bdLon:(double)lon
{
if (lon < RANGE_LON_MIN || lon > RANGE_LON_MAX)
return true;
if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
return true;
return false;
}
+ (CLLocationCoordinate2D)gcj02Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D resPoint;
double mgLat;
double mgLon;
if ([self outOfChina:ggLat bdLon:ggLon]) {
resPoint.latitude = ggLat;
resPoint.longitude = ggLon;
return resPoint;
}
double dLat = [self transformLat:(ggLon - 105.0)bdLon:(ggLat - 35.0)];
double dLon = [self transformLon:(ggLon - 105.0) bdLon:(ggLat - 35.0)];
double radLat = ggLat / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - jzEE * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((jzA * (1 - jzEE)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (jzA / sqrtMagic * cos(radLat) * M_PI);
mgLat = ggLat + dLat;
mgLon = ggLon + dLon;
resPoint.latitude = mgLat;
resPoint.longitude = mgLon;
return resPoint;
}
+ (CLLocationCoordinate2D)gcj02Decrypt:(double)gjLat gjLon:(double)gjLon {
CLLocationCoordinate2D gPt = [self gcj02Encrypt:gjLat bdLon:gjLon];
double dLon = gPt.longitude - gjLon;
double dLat = gPt.latitude - gjLat;
CLLocationCoordinate2D pt;
pt.latitude = gjLat - dLat;
pt.longitude = gjLon - dLon;
return pt;
}
+ (CLLocationCoordinate2D)bd09Decrypt:(double)bdLat bdLon:(double)bdLon
{
CLLocationCoordinate2D gcjPt;
double x = bdLon - 0.0065, y = bdLat - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);
double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);
gcjPt.longitude = z * cos(theta);
gcjPt.latitude = z * sin(theta);
return gcjPt;
}
+(CLLocationCoordinate2D)bd09Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D bdPt;
double x = ggLon, y = ggLat;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * M_PI);
double theta = atan2(y, x) + 0.000003 * cos(x * M_PI);
bdPt.longitude = z * cos(theta) + 0.0065;
bdPt.latitude = z * sin(theta) + 0.006;
return bdPt;
}
+ (CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location
{
return [self gcj02Encrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)gcj02ToWgs84:(CLLocationCoordinate2D)location
{
return [self gcj02Decrypt:location.latitude gjLon:location.longitude];
}
+ (CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02Pt = [self gcj02Encrypt:location.latitude
bdLon:location.longitude];
return [self bd09Encrypt:gcj02Pt.latitude bdLon:gcj02Pt.longitude] ;
}
+ (CLLocationCoordinate2D)gcj02ToBd09:(CLLocationCoordinate2D)location
{
return [self bd09Encrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)bd09ToGcj02:(CLLocationCoordinate2D)location
{
return [self bd09Decrypt:location.latitude bdLon:location.longitude];
}
+ (CLLocationCoordinate2D)bd09ToWgs84:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02 = [self bd09ToGcj02:location];
return [self gcj02Decrypt:gcj02.latitude gjLon:gcj02.longitude];
}
@end
Swift:
import Foundation
import CoreLocation
let a: Double = 6378245.0
let ee: Double = 0.00669342162296594323
// --- transform_earth_from_mars end ---
// --- transform_mars_vs_bear_paw ---
// 參考來源:http://blog.woodbunny.com/post-68.html
let x_pi: Double = .pi * 3000.0 / 180.0
extension CLLocation {
//地球座標(WGS84)
//國際標準
//例如:CLLocationManager
//火星座標 (GCJ-02)
//中國標準
//例如:iOS MKMapView、高德地圖、國內google 、搜搜、阿里雲
//百度座標 (BD-09)
//百度標準
//例如:百度 SDK,地圖
/// 從地圖座標轉化到火星座標
///
/// - Returns: CLLocation
func locationMarsFromEarth() -> CLLocationCoordinate2D {
let (n_lat,n_lng) = CLLocation.transform_earth_from_mars(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
let coord_2d = CLLocationCoordinate2D(latitude: n_lat + self.coordinate.latitude, longitude: n_lng + self.coordinate.longitude)
return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp).coordinate
}
/// 從火星座標到地圖座標
///
/// - Returns: CLLocation
func locationEarthFromMars() -> CLLocationCoordinate2D {
let (n_lat,n_lng) = CLLocation.transform_earth_from_mars(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
let coord_2d = CLLocationCoordinate2D(latitude: self.coordinate.latitude - n_lat, longitude: self.coordinate.longitude - n_lng)
return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp).coordinate
}
/// 從火星座標轉化到百度座標
///
/// - Returns: CLLocation
func locationBaiduFromMars() -> CLLocationCoordinate2D {
let (n_lat,n_lng) = CLLocation.transform_mars_from_baidu(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
let coord_2d = CLLocationCoordinate2D(latitude: n_lat, longitude: n_lng)
return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp).coordinate
}
/// 從百度座標到火星座標
///
/// - Returns: CLLocation
func locationMarsFromBaidu() -> CLLocationCoordinate2D {
let (n_lat,n_lng) = CLLocation.transform_baidu_from_mars(lat: self.coordinate.latitude, lng: self.coordinate.longitude)
let coord_2d = CLLocationCoordinate2D(latitude: n_lat, longitude: n_lng)
return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp).coordinate
}
/// 從百度座標到地圖座標
///
/// - Returns: CLLocation
func locationEarthFromBaidu() -> CLLocationCoordinate2D {
let mars = self.locationMarsFromBaidu()
let (n_lat,n_lng) = CLLocation.transform_earth_from_mars(lat: mars.latitude, lng: mars.longitude)
let coord_2d = CLLocationCoordinate2D(latitude: self.coordinate.latitude - n_lat, longitude: self.coordinate.longitude - n_lng)
return CLLocation(coordinate: coord_2d, altitude: self.altitude, horizontalAccuracy: self.horizontalAccuracy, verticalAccuracy: self.horizontalAccuracy, course: self.course, speed: self.speed, timestamp: self.timestamp).coordinate
}
private class func transform_earth_from_mars(lat: Double, lng: Double) -> (Double, Double) {
if CLLocation.transform_sino_out_china(lat: lat, lng: lng) {
return (lat, lng)
}
var dLat = CLLocation.transform_earth_from_mars_lat(lng - 105.0, lat - 35.0)
var dLng = CLLocation.transform_earth_from_mars_lng(lng - 105.0, lat - 35.0)
let radLat = lat / 180.0 * .pi
var magic = sin(radLat)
magic = 1 - ee * magic * magic
let sqrtMagic:Double = sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * .pi)
dLng = (dLng * 180.0) / (a / sqrtMagic * cos(radLat) * .pi)
return (dLat,dLng)
}
private class func transform_earth_from_mars_lat(_ x: Double,_ y: Double) -> Double {
var ret: Double = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(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 class func transform_earth_from_mars_lng(_ x: Double,_ y: Double) -> Double {
var ret: Double = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(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
}
private class func transform_mars_from_baidu(lat: Double, lng: Double) -> (Double,Double) {
let x = lng , y = lat
let z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi)
let theta = atan2(y, x) + 0.000003 * cos(x * x_pi)
return (z * sin(theta) + 0.006, z * cos(theta) + 0.0065)
}
private class func transform_baidu_from_mars(lat: Double, lng: Double) -> (Double,Double) {
let x = lng - 0.0065 , y = lat - 0.006
let z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi)
let theta = atan2(y, x) - 0.000003 * cos(x * x_pi)
return (z * sin(theta), z * cos(theta))
}
private class func transform_sino_out_china(lat: Double, lng: Double) -> Bool {
if (lng < 72.004 || lng > 137.8347) {
return true
}
if (lat < 0.8293 || lat > 55.8271) {
return true
}
return false
}
}