1,iOS導航SDK使用cocoapods
現在應該是 pod 'BaiduMapNavSDK'
2,iOS導航SDK默認的導航方式為:駕駛導航,暫時沒有發現騎行和步行導航。(所以,還是集成iOS地圖,什么都有了。)
3,打開網頁版百度、高德、系統地圖。
參考一:IOS_URI跳轉方式多種地圖導航的代碼實踐
參考二:iOS如何調起地圖進行導航(高德,百度,系統自帶
4,坐標轉換參考1
/**
1、地球坐標 :( 代號:GPS、WGS84 )--- 有W就是世界通用的
也就是原始坐標體系,這是國際公認的世界標準坐標體系;
注意:>1,使用 WGS84 坐標系統的產品有: 蘋果的 CLLocationManager 獲取的坐標
2,百度坐標系統 (代號:BD-09)
3,火星坐標: (代號:GCJ-02)--- G國家 C測繪 J局 02年測繪的
注意:>1,使用 GCJ-02 火星坐標系統的產品有:
高德地圖、騰訊地圖、阿里云地圖、靈圖51地圖
注意:現在蘋果系統自帶的地圖使用的是高德地圖,所以蘋果自帶的地圖應用,用的是GCJ-02的坐標系統。
!!!但是代碼中CLLocationManager獲取到的是WGS84坐標系的坐標
//---------------------------------------------------------------
地球坐標 ---> 火星坐標
*/
+ (CLLocationCoordinate2D)jq_transitionToHuoXingCoordinate:(CLLocationCoordinate2D)coordinate
{
double longitude = coordinate.longitude;
double latitude = coordinate.latitude;
// 首先判斷坐標是否屬于天朝
if (![self isInChinaWithlat:latitude lon:longitude]) {
CLLocationCoordinate2D resultCoordinate;
resultCoordinate.latitude = latitude;
resultCoordinate.longitude = longitude;
return resultCoordinate;
}
double a = 6378245.0;
double ee = 0.00669342162296594323;
double dLat = [self transform_earth_from_mars_lat_lat:(latitude - 35.0) lon:(longitude - 35.0)];
double dLon = [self transform_earth_from_mars_lng_lat:(latitude - 35.0) lon:(longitude - 35.0)];
double radLat = latitude / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
double newLatitude = latitude + dLat;
double newLongitude = longitude + dLon;
CLLocationCoordinate2D resultCoordinate;
resultCoordinate.longitude = newLongitude;
resultCoordinate.latitude = newLatitude;
return resultCoordinate;
}
+ (BOOL)isInChinaWithlat:(double)lat lon:(double)lon {
if (lon < 72.004 || lon > 137.8347)
return NO;
if (lat < 0.8293 || lat > 55.8271)
return NO;
return YES;
}
+ (double)transform_earth_from_mars_lat_lat:(double)y lon:(double)x {
double ret = -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 * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * M_PI) + 3320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
return ret;
}
+ (double)transform_earth_from_mars_lng_lat:(double)y lon:(double)x {
double ret = 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 * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
return ret;
}
/**
百度坐標轉高德坐標
*/
+ (CLLocationCoordinate2D)GCJ02FromBD09:(CLLocationCoordinate2D)coor
{
CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude - 0.0065, y = coor.latitude - 0.006;
CLLocationDegrees z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
CLLocationDegrees gg_lon = z * cos(theta);
CLLocationDegrees gg_lat = z * sin(theta);
return CLLocationCoordinate2DMake(gg_lat, gg_lon); //注意這里反著傳經緯度
}
/**
高德坐標轉百度坐標
*/
+ (CLLocationCoordinate2D)BD09FromGCJ02:(CLLocationCoordinate2D)coor
{
CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude, y = coor.latitude;
CLLocationDegrees z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
CLLocationDegrees bd_lon = z * cos(theta) + 0.0065;
CLLocationDegrees bd_lat = z * sin(theta) + 0.006;
return CLLocationCoordinate2DMake(bd_lat, bd_lon); //注意這里反著傳經緯度
}
5,參數知識點介紹
/**
{
origin 起點名稱或經緯度,或者可同時提供名稱和經緯度,此時經緯度優先級高,將作為導航依據,名稱只負責展示。 必選
1、名稱:天安門
2、經緯度:39.98871<緯度>,116.43234<經度>。
3、名稱和經緯度:name:天安門|latlng:39.98871,116.43234(注意:“name:天安門|”是必須要寫的!)
}
{
region 城市名或縣名 必選
(當給定region時,認為起點和終點都在同一城市,除非單獨給定起點或終點的城市。)
}
name 線路名稱 必選
zoom 展現地圖的級別,默認為視覺最優級別。 可選
src 調用來源,規則:webapp.line.yourCompanyName.yourAppName 必選
location lat<緯度>,lng<經度> 必選
title 標注點顯示標題 必選
product下可直接跟方法,當然產品線也可增加一個service級別
content 標注點顯示內容 必選
mode導航模式,固定為transit、driving、navigation、walking,riding分別表示公交、駕車、導航、步行和騎行
{
coord_type 坐標類型,可選參數,默認為bd09ll。 可選
允許的值為bd09ll、bd09mc、gcj02、wgs84。
bd09ll表示百度經緯度坐標,
bd09mc表示百度墨卡托坐標,
gcj02表示經過國測局加密的坐標,
wgs84表示gps獲取的坐標。
}
舉例:
baidumap://map/direction?origin=中關村&destination=五道口&mode=driving®ion=北京
//本示例是通過該URL啟動地圖app并進入北京市從中關村到五道口的駕車導航路線圖
*/
#工具類方法:
//
// XWMapTool.m
// LeSong
//
// Created by 李學文 on 2017/6/12.
// Copyright ? 2017年 李學文. All rights reserved.
//
#import "XWMapTool.h"
#import <MapKit/MapKit.h>
#import "LSGetCurrentLocation.h"
@implementation XWMapTool
//防止獲取當前經緯度時多次回調。
static NSString *isFirst;
+(void)jq_navigationToLocation:(CLLocationCoordinate2D)endCoordinate2D
{
//http://blog.csdn.net/a416863220/article/details/51220739
isFirst = @"1";
//獲取自己的當前位置
[[LSGetCurrentLocation shareManager] beginLocate];
[LSGetCurrentLocation shareManager].locationBlock = ^(double longitude, double latitude) {
if (longitude>=0&&latitude>=0) {
[[LSGetCurrentLocation shareManager] stopLocation];//結束定位
if ([isFirst integerValue]) {
isFirst = @"0";
CLLocationCoordinate2D startCoordinate2D;
startCoordinate2D.latitude = latitude;
startCoordinate2D.longitude = longitude;
//把開始和結束的位置傳過去,返回可以打開的APP以及對應的URL
NSArray * arry = [self jq_startNavgationStartLocation:startCoordinate2D endLocation:endCoordinate2D];
//初始化提示框;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選擇導航地圖" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for (int i=0; i<arry.count; i++) {
NSDictionary *dic = [arry objectAtIndex:i];
NSString *title = [dic objectForKey:@"title"];
[alert addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = dic[@"url"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}]];
}
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
//彈出提示框;
[self.jq_currentVC presentViewController:alert animated:true completion:nil];
}
}
};
}
+(NSMutableArray *)jq_startNavgationStartLocation:(CLLocationCoordinate2D)startCoordinate2D endLocation:(CLLocationCoordinate2D)endCoordinate2D
{
/**
{
origin 起點名稱或經緯度,或者可同時提供名稱和經緯度,此時經緯度優先級高,將作為導航依據,名稱只負責展示。 必選
1、名稱:天安門
2、經緯度:39.98871<緯度>,116.43234<經度>。
3、名稱和經緯度:name:天安門|latlng:39.98871,116.43234(注意:“name:天安門|”是必須要寫的!)
}
{
region 城市名或縣名 必選
(當給定region時,認為起點和終點都在同一城市,除非單獨給定起點或終點的城市。)
}
name 線路名稱 必選
zoom 展現地圖的級別,默認為視覺最優級別。 可選
src 調用來源,規則:webapp.line.yourCompanyName.yourAppName 必選
location lat<緯度>,lng<經度> 必選
title 標注點顯示標題 必選
product下可直接跟方法,當然產品線也可增加一個service級別
content 標注點顯示內容 必選
mode導航模式,固定為transit、driving、navigation、walking,riding分別表示公交、駕車、導航、步行和騎行
{
coord_type 坐標類型,可選參數,默認為bd09ll。 可選
允許的值為bd09ll、bd09mc、gcj02、wgs84。
bd09ll表示百度經緯度坐標,
bd09mc表示百度墨卡托坐標,
gcj02表示經過國測局加密的坐標,
wgs84表示gps獲取的坐標。
}
舉例:
baidumap://map/direction?origin=中關村&destination=五道口&mode=driving®ion=北京
//本示例是通過該URL啟動地圖app并進入北京市從中關村到五道口的駕車導航路線圖
*/
NSMutableArray *maps = [NSMutableArray array];
//百度地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
baiduMapDic[@"title"] = @"百度地圖";
CLLocationCoordinate2D baiduStartCoordinate = startCoordinate2D;
CLLocationCoordinate2D baiduEndCoordinate = endCoordinate2D;
//地球坐標轉火星坐標
baiduStartCoordinate = [self jq_transitionToHuoXingCoordinate:baiduStartCoordinate];
//高德坐標轉百度坐標
baiduStartCoordinate = [self BD09FromGCJ02:baiduStartCoordinate];
baiduEndCoordinate = [self BD09FromGCJ02:baiduEndCoordinate];
/**
注意:“name:當前位置|”必須要在latlng之前寫上!!!
>1,起始位置:origin=name:當前位置|latlng:%f,%f
>2,終點位置:destination=name:終點位置|latlng:%lf,%lf
*/
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=name:當前位置|latlng:%f,%f&destination=name:終點位置|latlng:%lf,%lf&mode=riding&coord_type=gcj02",baiduStartCoordinate.latitude,baiduStartCoordinate.longitude,baiduEndCoordinate.latitude,baiduEndCoordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
baiduMapDic[@"url"] = urlString;
[maps addObject:baiduMapDic];
}
//高德地圖
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
gaodeMapDic[@"title"] = @"高德地圖";
/**
sourceApplication : APP名稱
backScheme : URL Scheme(用于從高德返回到APP,唯一標識)
lat:緯度
lon:經度
dev和style固定傳就可以了
*/
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%.1f&lon=%.1f&dev=0&style=2",@"樂送APP",@"GaoDeMap1001",endCoordinate2D.latitude,endCoordinate2D.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
gaodeMapDic[@"url"] = urlString;
[maps addObject:gaodeMapDic];
}
// //谷歌地圖
// if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
// NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
// googleMapDic[@"title"] = @"谷歌地圖";
// NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"導航測試",@"nav123456",endCoordinate2D.latitude, endCoordinate2D.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// googleMapDic[@"url"] = urlString;
// [maps addObject:googleMapDic];
// }
//
// //騰訊地圖
// if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
// NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
// qqMapDic[@"title"] = @"騰訊地圖";
// NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=終點&coord_type=1&policy=0",endCoordinate2D.latitude, endCoordinate2D.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// qqMapDic[@"url"] = urlString;
// [maps addObject:qqMapDic];
// }
return maps;
}
/**
1、地球坐標 :( 代號:GPS、WGS84 )--- 有W就是世界通用的
也就是原始坐標體系,這是國際公認的世界標準坐標體系;
注意:>1,使用 WGS84 坐標系統的產品有: 蘋果的 CLLocationManager 獲取的坐標
2,百度坐標系統 (代號:BD-09)
3,火星坐標: (代號:GCJ-02)--- G國家 C測繪 J局 02年測繪的
注意:>1,使用 GCJ-02 火星坐標系統的產品有:
高德地圖、騰訊地圖、阿里云地圖、靈圖51地圖
注意:現在蘋果系統自帶的地圖使用的是高德地圖,所以蘋果自帶的地圖應用,用的是GCJ-02的坐標系統。
!!!但是代碼中CLLocationManager獲取到的是WGS84坐標系的坐標
//---------------------------------------------------------------
地球坐標 ---> 火星坐標
*/
+ (CLLocationCoordinate2D)jq_transitionToHuoXingCoordinate:(CLLocationCoordinate2D)coordinate
{
double longitude = coordinate.longitude;
double latitude = coordinate.latitude;
// 首先判斷坐標是否屬于天朝
if (![self isInChinaWithlat:latitude lon:longitude]) {
CLLocationCoordinate2D resultCoordinate;
resultCoordinate.latitude = latitude;
resultCoordinate.longitude = longitude;
return resultCoordinate;
}
double a = 6378245.0;
double ee = 0.00669342162296594323;
double dLat = [self transform_earth_from_mars_lat_lat:(latitude - 35.0) lon:(longitude - 35.0)];
double dLon = [self transform_earth_from_mars_lng_lat:(latitude - 35.0) lon:(longitude - 35.0)];
double radLat = latitude / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
double newLatitude = latitude + dLat;
double newLongitude = longitude + dLon;
CLLocationCoordinate2D resultCoordinate;
resultCoordinate.longitude = newLongitude;
resultCoordinate.latitude = newLatitude;
return resultCoordinate;
}
+ (BOOL)isInChinaWithlat:(double)lat lon:(double)lon {
if (lon < 72.004 || lon > 137.8347)
return NO;
if (lat < 0.8293 || lat > 55.8271)
return NO;
return YES;
}
+ (double)transform_earth_from_mars_lat_lat:(double)y lon:(double)x {
double ret = -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 * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * M_PI) + 3320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
return ret;
}
+ (double)transform_earth_from_mars_lng_lat:(double)y lon:(double)x {
double ret = 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 * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
return ret;
}
/**
百度坐標轉高德坐標
*/
+ (CLLocationCoordinate2D)GCJ02FromBD09:(CLLocationCoordinate2D)coor
{
CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude - 0.0065, y = coor.latitude - 0.006;
CLLocationDegrees z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
CLLocationDegrees gg_lon = z * cos(theta);
CLLocationDegrees gg_lat = z * sin(theta);
return CLLocationCoordinate2DMake(gg_lat, gg_lon); //注意這里反著傳經緯度
}
/**
高德坐標轉百度坐標
*/
+ (CLLocationCoordinate2D)BD09FromGCJ02:(CLLocationCoordinate2D)coor
{
CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude, y = coor.latitude;
CLLocationDegrees z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
CLLocationDegrees bd_lon = z * cos(theta) + 0.0065;
CLLocationDegrees bd_lat = z * sin(theta) + 0.006;
return CLLocationCoordinate2DMake(bd_lat, bd_lon); //注意這里反著傳經緯度
}
@end