打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS 调用地图导航
在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少
[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以下,调用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://"]])
参考
示例代码
[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",
@"云华时代", 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
[cpp] view plaincopy
[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
[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以下,调用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];
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS网络收音机
iOS开发中的网络请求
异步POST请求和同步请求
NSURLConnection的使用
App间相互跳转及图片分享
iOS开发 判断当前APP版本和升级
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服