生活随笔
收集整理的這篇文章主要介紹了
iOS 调用地图导航
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在IOS6.0系統(tǒng)后,兼容iOS5.0與iOS6.0地圖導(dǎo)航,需要分兩個(gè)步驟
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用來獲取手機(jī)的系統(tǒng),判斷系統(tǒng)是多少
[cpp]?view plaincopy
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以下,調(diào)用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?{?//?直接調(diào)用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、高德等
使用前,先判斷設(shè)備上是否已安裝應(yīng)用
百度地圖:
if?([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://map/"]])
參考
高德地圖:
if?([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]])
參考
Google Maps:
if?([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]])
參考
示例代碼
[cpp]?view plaincopy
-?(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",?? ???????????????????????????????@"云華時(shí)代",?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];?? ????}?? }??
顯示一個(gè)ActionSheet
[cpp]?view plaincopy
[self?availableMapsApps];?? ????UIActionSheet?*action?=?[[UIActionSheet?alloc]?init];?? ?????? ????[action?addButtonWithTitle:@"使用系統(tǒng)自帶地圖導(dǎo)航"];?? ????for?(NSDictionary?*dic?in?self.availableMaps)?{?? ????????[action?addButtonWithTitle:[NSString?stringWithFormat:@"使用%@導(dǎo)航",?dic[@"name"]]];?? ????}?? ????[action?addButtonWithTitle:@"取消"];?? ????action.cancelButtonIndex?=?self.availableMaps.count?+?1;?? ????action.delegate?=?self;?? ????[action?showInView:self.view];??
實(shí)現(xiàn)delegate
[cpp]?view plaincopy
-?(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以下,調(diào)用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{//?直接調(diào)用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];?? ????}?? } ?
總結(jié)
以上是生活随笔為你收集整理的iOS 调用地图导航的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。