iOS開發:采用URI方式跳轉到各類地圖進行導航

  • 最近在做導航,所以把自己找到的資料總結一下!

  • 無論是百度地圖、高德地圖、谷歌地圖還是騰訊地圖它們都有自己的SDK,我們只需要在自己的工程中導入SDK并查看相應的官方文檔,基本上就可以實現導航。但是這樣每個地圖的SDK都導入不但麻煩而且占用APP的內存。最關鍵的是我們上傳到AppStore的包文件是有限制的。所以我的原則是能不導入的SDK 就不導入。

  • 還有一種方式就是是以URI跳轉的方式(在iOS中就是以URL Scheme的方式),直接跳到對應的地圖APP中,直接利用對方的功能來導航。缺點是用戶沒有安裝對應的APP就不能使用其進行導航。 點擊導航按鈕會出現如下的彈窗, 當然手機上未安裝的地圖 其名稱就不會出現在彈窗上。

    1439521824220516.png

  • 在 iOS9之后 若想用URI方式跳轉到百度地圖、高德地圖、騰訊地圖、谷歌地圖,需要你在info.plist加入這些東西。(ps:LSApplicationQueriesSchemes,短的自己手打吧,另外注意類型?。?/p>

    20160422164514276.png

以下出行的默認方式都是駕車

一、百度地圖

  1. 說到百度地圖,就不得不說它很坑爹。因為百度地圖獲取的經緯度,是在GCJ-02(火星坐標)進行偏移得到的經緯度,而高德、谷歌、騰訊都是使用GCJ-02坐標體系得到的經緯度。這樣使用百度地圖獲取到的經緯度在高德、谷歌、騰訊上導航都會出現很大的偏差。所以自己做的APP中需要地圖功能最好不要導入百度地圖的SDK(使用上面三個中任何一個地圖獲取到的經緯度都可以很容易的轉換成百度地圖需要的經緯度),如果你是像我這樣中途接手的項目,百度地圖的相應功能已經做好了,那你可以用下面的方式換算一下經緯度(最下方)。
  2. 代碼如下 :需傳入起點和終點的經緯度
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
        UIAlertAction *baiduMapAction = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:終點&mode=driving";
            NSString *urlString = [[NSString stringWithFormat:
                                    baiduParameterFormat,
                                    userLocation.location.coordinate.latitude,
                                    userLocation.location.coordinate.longitude,
                                    self.destinationCoordinate.latitude,
                                    self.destinationCoordinate.longitude]
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }];
        [actionSheet addAction:baiduMapAction];
    }
  1. 各個參數代表的含義可參考百度地圖官方文檔

二、高德地圖

  1. 只需傳入終點經緯度 高德地圖能夠跳轉回你的APP,前提是backScheme=%@(你的APP的URL)要填寫。代碼如下
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://map/"]]) {
        UIAlertAction *gaodeMapAction = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *gaodeParameterFormat = @"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2";
            NSString *urlString = [[NSString stringWithFormat:
                                    gaodeParameterFormat,
                                    @"yourAppName",
                                    @"yourAppUrlSchema",
                                    @"終點",
                                    self.destinationCoordinate.latitude,
                                    self.destinationCoordinate.longitude]
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }];
        [actionSheet addAction:gaodeMapAction];
    }
  1. 各個參數的含義可參考高德地圖官方文檔

三、蘋果地圖

  1. 需傳入起點和終點的經緯度,并導入頭文件#import MapKit/MKMapItem.h>
[actionSheet addAction:[UIAlertAction actionWithTitle:@"蘋果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //起點
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake(self.destinationCoordinate.latitude, self.destinationCoordinate.longitude);
        //終點
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];
        //默認駕車
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                       launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                       MKLaunchOptionsMapTypeKey:[NSNumber numberWithInteger:MKMapTypeStandard],
                                       MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
    }]];
  1. 各個參數的含義可參考蘋果地圖官方文檔

四、谷歌地圖

  1. 只需傳入終點的經緯度
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://map/"]]) {
        [actionSheet addAction:[UIAlertAction actionWithTitle:@"蘋果地圖"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",
                                    appName,
                                    urlScheme,
                                    coordinate.latitude,
                                    coordinate.longitude]
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }]];
    }
  1. 各個參數的含義可參考谷歌地圖官方文檔

五、騰訊地圖

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://map/"]]) {        
        [actionSheet addAction:[UIAlertAction actionWithTitle:@"騰訊地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *QQParameterFormat = @"qqmap://map/routeplan?type=drive&fromcoord=%f, %f&tocoord=%f,%f&coord_type=1&policy=0&refer=%@";
            NSString *urlString = [[NSString stringWithFormat:
                                    QQParameterFormat,
                                    userLocation.location.coordinate.latitude,
                                    userLocation.location.coordinate.longitude,
                                    self.destinationCoordinate.latitude,
                                    self.destinationCoordinate.longitude,
                                    @"yourAppName"]
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }]];
    }

GCJ-02坐標轉換成BD-09坐標 和逆轉換

  • GCJ-02坐標轉換為BD-09坐標
/** *  將GCJ-02坐標轉換為BD-09坐標 即將高德地圖上獲取的坐標轉換成百度坐標 */
- (CLLocationCoordinate2D)gcj02CoordianteToBD09:(CLLocationCoordinate2D)gdCoordinate
{
    double x_PI = M_PI * 3000.0 /180.0;
    
    double gd_lat = gdCoordinate.latitude;
    
    double gd_lon = gdCoordinate.longitude;
    
    double z = sqrt(gd_lat * gd_lat + gd_lon * gd_lon) + 0.00002 * sin(gd_lat * x_PI);
    
    double theta = atan2(gd_lat, gd_lon) + 0.000003 * cos(gd_lon * x_PI);
    
    return CLLocationCoordinate2DMake(z * sin(theta) + 0.006, z * cos(theta) + 0.0065);
}
  • BD-09坐標轉換為GCJ-02坐標
/** *  將BD-09坐標轉換為GCJ-02坐標 即將百度地圖上獲取的坐標轉換成高德地圖的坐標 */
- (CLLocationCoordinate2D)bd09CoordinateToGCJ02:(CLLocationCoordinate2D)bdCoordinate
{
    double x_PI = M_PI * 3000.0 /180.0;
    
    double bd_lat = bdCoordinate.latitude - 0.006;
    
    double bd_lon = bdCoordinate.longitude - 0.0065;
    
    double z = sqrt(bd_lat * bd_lat + bd_lon * bd_lon) - 0.00002 * sin(bd_lat * x_PI);
    
    double theta = atan2(bd_lat, bd_lon) - 0.000003 * cos(bd_lon * x_PI);
    
    return CLLocationCoordinate2DMake(z * sin(theta), z * cos(theta));
}

地圖坐標系轉換

#import <CoreLocation/CoreLocation.h>
/*
 從 CLLocationManager 取出來的經緯度放到 mapView 上顯示,是錯誤的!
 從 CLLocationManager 取出來的經緯度去 Google Maps API 做逆地址解析,當然是錯的!
 從 MKMapView 取出來的經緯度去 Google Maps API 做逆地址解析終于對了。去百度地圖API做逆地址解析,依舊是錯的!
 從上面兩處取的經緯度放到百度地圖上顯示都是錯的!錯的!的!
 
 分為 地球坐標,火星坐標(iOS mapView 高德 , 國內google ,搜搜、阿里云 都是火星坐標),百度坐標(百度地圖數據主要都是四維圖新提供的)
 
 火星坐標: MKMapView
 地球坐標: CLLocationManager
 
 當用到CLLocationManager 得到的數據轉化為火星坐標, MKMapView不用處理
 
 
 API                坐標系
 百度地圖API         百度坐標
 騰訊搜搜地圖API      火星坐標
 搜狐搜狗地圖API      搜狗坐標
 阿里云地圖API       火星坐標
 圖吧MapBar地圖API   圖吧坐標
 高德MapABC地圖API   火星坐標
 靈圖51ditu地圖API   火星坐標
 */
@interface CLLocation (Location)

//從地圖坐標轉化到火星坐標
- (CLLocation *)locationMarsFromEarth;

//從火星坐標轉化到百度坐標
- (CLLocation *)locationBaiduFromMars;

//從百度坐標到火星坐標
- (CLLocation *)locationMarsFromBaidu;

//從火星坐標到地圖坐標
- (CLLocation *)locationEarthFromMars;

//從百度坐標到地圖坐標
- (CLLocation *)locationEarthFromBaidu;

@end
#import "CLLocation+Location.h"

void transform_earth_from_mars(double lat, double lng, double* tarLat, double* tarLng);
void transform_mars_from_baidu(double lat, double lng, double* tarLat, double* tarLng);
void transform_baidu_from_mars(double lat, double lng, double* tarLat, double* tarLng);

@implementation CLLocation (Location)

- (CLLocation*)locationMarsFromEarth
{
    double lat = 0.0;
    double lng = 0.0;
    transform_earth_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
    return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat+self.coordinate.latitude, lng+self.coordinate.longitude)
                                         altitude:self.altitude
                               horizontalAccuracy:self.horizontalAccuracy
                                 verticalAccuracy:self.verticalAccuracy
                                           course:self.course
                                            speed:self.speed
                                        timestamp:self.timestamp];
}

- (CLLocation*)locationEarthFromMars
{
    double lat = 0.0;
    double lng = 0.0;
    transform_earth_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
    return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(self.coordinate.latitude-lat, self.coordinate.longitude-lng)
                                         altitude:self.altitude
                               horizontalAccuracy:self.horizontalAccuracy
                                 verticalAccuracy:self.verticalAccuracy
                                           course:self.course
                                            speed:self.speed
                                        timestamp:self.timestamp];
    return nil;
}

- (CLLocation*)locationBaiduFromMars
{
    double lat = 0.0;
    double lng = 0.0;
    transform_mars_from_baidu(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
    return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                         altitude:self.altitude
                               horizontalAccuracy:self.horizontalAccuracy
                                 verticalAccuracy:self.verticalAccuracy
                                           course:self.course
                                            speed:self.speed
                                        timestamp:self.timestamp];
}

- (CLLocation*)locationMarsFromBaidu
{
    double lat = 0.0;
    double lng = 0.0;
    transform_baidu_from_mars(self.coordinate.latitude, self.coordinate.longitude, &lat, &lng);
    return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                         altitude:self.altitude
                               horizontalAccuracy:self.horizontalAccuracy
                                 verticalAccuracy:self.verticalAccuracy
                                           course:self.course
                                            speed:self.speed
                                        timestamp:self.timestamp];
}

-(CLLocation*)locationEarthFromBaidu
{
    double lat = 0.0;
    double lng = 0.0;
    CLLocation *Mars = [self locationMarsFromBaidu];
    
    transform_earth_from_mars(Mars.coordinate.latitude, Mars.coordinate.longitude, &lat, &lng);
    return [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(Mars.coordinate.latitude-lat, Mars.coordinate.longitude-lng)
                                         altitude:self.altitude
                               horizontalAccuracy:self.horizontalAccuracy
                                 verticalAccuracy:self.verticalAccuracy
                                           course:self.course
                                            speed:self.speed
                                        timestamp:self.timestamp];
    return nil;
}

@end


// --- transform_earth_from_mars ---
// 參考來源:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936
// Krasovsky 1940
//
// a = 6378245.0, 1/f = 298.3
// b = a * (1 - f)
// ee = (a^2 - b^2) / a^2;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;

bool transform_sino_out_china(double lat, double lon)
{
    if (lon < 72.004 || lon > 137.8347)
        return true;
    if (lat < 0.8293 || lat > 55.8271)
        return true;
    return false;
}

double transform_earth_from_mars_lat(double x, double y)
{
    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) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

double transform_earth_from_mars_lng(double x, double y)
{
    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;
}

void transform_earth_from_mars(double lat, double lng, double* tarLat, double* tarLng)
{
    if (transform_sino_out_china(lat, lng))
    {
        *tarLat = lat;
        *tarLng = lng;
        return;
    }
    double dLat = transform_earth_from_mars_lat(lng - 105.0, lat - 35.0);
    double dLon = transform_earth_from_mars_lng(lng - 105.0, lat - 35.0);
    double radLat = lat / 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);
    *tarLat = dLat;
    *tarLng = dLon;
}

// --- transform_earth_from_mars end ---
// --- transform_mars_vs_bear_paw ---
// 參考來源:http://blog.woodbunny.com/post-68.html
const double x_pi = M_PI * 3000.0 / 180.0;

void transform_mars_from_baidu(double gg_lat, double gg_lon, double *bd_lat, double *bd_lon)
{
    double x = gg_lon, y = gg_lat;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
    *bd_lon = z * cos(theta) + 0.0065;
    *bd_lat = z * sin(theta) + 0.006;
}

void transform_baidu_from_mars(double bd_lat, double bd_lon, double *gg_lat, double *gg_lon)
{
    double x = bd_lon - 0.0065, y = bd_lat - 0.006;
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
    *gg_lon = z * cos(theta);
    *gg_lat = z * sin(theta);
}
  • Tips:無論導入的是百度SDK還是高德SDK,他們內部都封裝了將目標經緯度轉換為高德坐標系或百度坐標系(文檔上的接口可能被棄用沒有及時更新,是不是很坑爹),但是沒有將高德或百度坐標轉換為別的坐標系下的坐標的接口。
  • 設置URL Scheme:http://blog.csdn.net/wm9028/article/details/49995329
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,983評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,772評論 3 422
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,947評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,201評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,960評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,350評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,406評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,549評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,104評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,914評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,089評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,647評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,340評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,753評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,007評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,834評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,106評論 2 375

推薦閱讀更多精彩內容