iOS 調用地圖導航

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   os   ar   使用   for   

在IOS6.0系統後,相容iOS5.0與iOS6.0地圖導航,需要分兩個步驟

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用來擷取手機的系統,判斷系統是多少

    CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);        if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,調用google map                NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];        //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude        urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];        NSURL *aURL = [NSURL URLWithString:urlString];        [[UIApplication sharedApplication] openURL:aURL];    } else { // 直接調用ios自己帶的apple map                MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];        toLocation.name = @"to name";                [MKMapItem openMapsWithItems:@[currentLocation, toLocation]                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];            }

  

如果不想使用蘋果內建的地圖的話,也可以使用第三方的地圖,如百度、Google Maps、高德等

使用前,先判斷裝置上是否已安裝應用

百度地圖:

 

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://map/"]])

參考

 

高德地圖:

 

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]])

參考

 

Google Maps:

 

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]])

參考

 

 

範例程式碼

- (void)availableMapsApps {    [self.availableMaps removeAllObjects];        CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);    NSString *toName = @"to name";        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){        NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:%@&mode=transit",                               startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName];                NSDictionary *dic = @{@"name": @"百度地圖",                              @"url": urlString};        [self.availableMaps addObject:dic];    }    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {        NSString *urlString = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3",                               @"雲華時代", endCoor.latitude, endCoor.longitude];                NSDictionary *dic = @{@"name": @"高德地圖",                              @"url": urlString};        [self.availableMaps addObject:dic];    }    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {        NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude];                NSDictionary *dic = @{@"name": @"Google Maps",                              @"url": urlString};        [self.availableMaps addObject:dic];    }}

  顯示一個ActionSheet

[self availableMapsApps];    UIActionSheet *action = [[UIActionSheet alloc] init];        [action addButtonWithTitle:@"使用系統內建地圖導航"];    for (NSDictionary *dic in self.availableMaps) {        [action addButtonWithTitle:[NSString stringWithFormat:@"使用%@導航", dic[@"name"]]];    }    [action addButtonWithTitle:@"取消"];    action.cancelButtonIndex = self.availableMaps.count + 1;    action.delegate = self;    [action showInView:self.view];

  實現delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {    if (buttonIndex == 0) {        CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;        CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);                if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下,調用google map                        NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];            //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude            urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];            NSURL *aURL = [NSURL URLWithString:urlString];            [[UIApplication sharedApplication] openURL:aURL];        } else{// 直接調用ios自己帶的apple map                        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];            MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil];            MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];            toLocation.name = @"to name";                        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]                           launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];                    }    }else if (buttonIndex < self.availableMaps.count+1) {        NSDictionary *mapDic = self.availableMaps[buttonIndex-1];        NSString *urlString = mapDic[@"url"];        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];        NSURL *url = [NSURL URLWithString:urlString];        DEBUG_LOG(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString);        [[UIApplication sharedApplication] openURL:url];    }}

  

iOS 調用地圖導航

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.