iOS 開發筆記 - 導航到地圖

導航到地圖,已經不是什麼新鮮事了。網上有好多參考的資料,我總結出只須要兩步ios

第一步:在info中加上支持的各平臺git

好比:iosamap高德地圖、comgooglemaps谷歌地圖、baidumap百度地圖、qqmap騰訊地圖app

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>iosamap</string>
        <string>comgooglemaps</string>
        <string>baidumap</string>
        <string>qqmap</string>
    </array>

第二步:直接在使用的地方,調用下面的代碼便可google

- (void)mapChooose {
    //系統版本高於8.0,使用UIAlertController
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"導航到設備" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //自帶地圖
    [alertController addAction:[UIAlertAction actionWithTitle:@"自帶地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"alertController -- 自帶地圖");
        //使用自帶地圖導航
        MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation];
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:nil]];
        [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
    }]];
    
    //判斷是否安裝了高德地圖,若是安裝了高德地圖,則使用高德地圖導航
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        [alertController addAction:[UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"alertController -- 高德地圖");
            NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",self.coordinate.latitude,self.coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication  sharedApplication]openURL:[NSURL URLWithString:urlsting]];
        }]];
    }
    
    //判斷是否安裝了百度地圖,若是安裝了百度地圖,則使用百度地圖導航
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        [alertController addAction:[UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"alertController -- 百度地圖");
            NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{個人位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",self.coordinate.latitude,self.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
        }]];
    }
    
    //判斷是否安裝了谷歌地圖,若是安裝了谷歌地圖,則使用谷歌地圖導航
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        [alertController 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,
                                    self.coordinate.latitude,
                                    self.coordinate.longitude]
                                   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }]];
    }
    
    //添加取消選項
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    //顯示alertController
    [self presentViewController:alertController animated:YES completion:nil];
}
相關文章
相關標籤/搜索