700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > iOS 高德地图 百度地图 以及苹果自带的地图URI的使用 以及CLLocationManager的使用

iOS 高德地图 百度地图 以及苹果自带的地图URI的使用 以及CLLocationManager的使用

时间:2021-06-29 22:57:17

相关推荐

iOS  高德地图 百度地图 以及苹果自带的地图URI的使用 以及CLLocationManager的使用

1.CLLocationManager的使用

1.首先Xcode导入一个自己带的官方头文件在任意一个类.h

#import<MapKit/MapKit.h>

并遵守两协议

<CLLocationManagerDelegate,UIActionSheetDelegate>

在创建

@property (strong,nonatomic)CLLocationManager* locationManager;double currentLatitude;//定位坐标double currentLongitude;double end_currentLongitude;//终点坐标double end_currentLatitude;NSString *address;//想去地点的名字

在该类的.m文件直接创建一个方法直接调用创建:

self.locationManager = [[CLLocationManager alloc]init];self.locationManager.delegate = self;self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;self.locationManager.distanceFilter = 100.0f;[self.locationManager startUpdatingLocation];

iOS8.0后加抢了对安全隐私访问权限,所以需要在加上

//请求权限[self.locationManager requestWhenInUseAuthorization];

//定位代理经纬度回调-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {// [self.locationManager stopUpdatingLocation];NSLog(@"=========================================%@",[NSStringstringWithFormat:@"经度:%3.5f纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);currentLatitude=newLocation.coordinate.latitude;//纬度currentLongitude=newLocation.coordinate.longitude;//经度}

info.plist文件也需要加两个字段 Privacy - Location Always Usage Description: // 这个在前台、后台都时时定位 如果不使用后台定位,可不添加这个字段,否则 App Store 审核或有问题,如果需要请坐声明描述:GPS在后台持续运行,可以大大降低电池的寿命。 Privacy - Location When In Use Usage Description 类型可以设置为字符串 // 这个在前台时 时时定位接下来便是应用高德,百度,苹果地图URI的使用

2.

//首先判断定义:BOOL hasBaiduMap ;//判断是否存在百度地图BOOL hasGaodeMap ;//判断是否存在高德地图NSString *address;//初始化hasBaiduMap=NO;hasGaodeMap=NO;

判断存在各种地图需要在info.plist中添加相应的字段:

百度添加:baidumap

高德添加:iosamap

直接调用方法判断或者在点击时间中进行下面判断

if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){hasBaiduMap =YES;}if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){hasGaodeMap =YES;}

//判断完成后执行[self getAllKindsOFMaps];-(void)getAllKindsOFMaps{if (hasBaiduMap&&hasGaodeMap ) {UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:addressdelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"高德地图中导航",@"百度地图中导航",@"苹果地图中导航",nil];actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;[actionSheet showInView:self.view];}elseif (hasBaiduMap){UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:addressdelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"百度地图中导航",@"苹果地图中导航",nil];actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;[actionSheet showInView:self.view];} else if (hasGaodeMap){UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:addressdelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"高德地图中导航",@"苹果地图中导航",nil];actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;[actionSheet showInView:self.view];}else{UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:addressdelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"苹果地图中导航",nil];actionSheet.actionSheetStyle =UIActionSheetStyleBlackOpaque;[actionSheet showInView:self.view];}}#pragma mark 打开地图导航-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{if (hasBaiduMap&&hasGaodeMap ) {if (buttonIndex ==0) {//高德地图中查看[self openGaoDeMap];}elseif(buttonIndex ==1) {//百度地图中查看[self openBaiDuMap];} else if(buttonIndex ==2) {//苹果地图中查看[self openAppleMap];}} else if (hasBaiduMap){if (buttonIndex ==0) {//百度地图中查看[self openBaiDuMap];} else if(buttonIndex ==1) {//苹果地图中查看[self openAppleMap];}} else if (hasGaodeMap){if (buttonIndex ==0) {[self openGaoDeMap];//高德地图中查看} else if(buttonIndex ==1) {//苹果地图中查看[self openAppleMap];}} else {if (buttonIndex ==0){//苹果地图中查看[self openAppleMap];}}}- (void)openBaiDuMap{NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving",currentLatitude,currentLongitude,end_currentLatitude,end_currentLongitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];}- (void)openGaoDeMap{NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2",@"app name", @"EasyToLearn_Coach",@"终点",end_currentLatitude,end_currentLongitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];[[UIApplication sharedApplication]openURL:[NSURLURLWithString:urlString]];}- (void)openAppleMap{//起点NSLog(@"%f%f",currentLatitude,currentLongitude);CLLocationCoordinate2D coords1 =CLLocationCoordinate2DMake(currentLatitude,currentLongitude);MKMapItem *currentLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:coords1addressDictionary:nil]];//目的地的位置CLLocationCoordinate2D coords2 =CLLocationCoordinate2DMake(end_currentLatitude,end_currentLongitude);MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:coords2addressDictionary:nil]];toLocation.name =address;NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation,nil];NSDictionary *options =@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],MKLaunchOptionsShowsTrafficKey:@YES};//打开苹果自身地图应用,并呈现特定的item[MKMapItem openMapsWithItems:itemslaunchOptions:options];}

代码实现效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。