iOS设备定位
2023-09-27 14:27:01 时间
一、iOS谈到定位
1、SignInSignOutViewController.h
@interface SignInSignOutViewController : UIViewController<CLLocationManagerDelegate>{ CLLocationManager *_locationManager; // 纬度 float _latitude; // 经度 float _longitude; } @property (nonatomic,retain) CLLocationManager *locationManager; @property (nonatomic) float latitude; @property (nonatomic) float longitude; @end
2、SignInSignOutViewController.m
#import "SignInSignOutViewController.h" @interface SignInSignOutViewController () @end @implementation SignInSignOutViewController @synthesize locationManager = _locationManager; @synthesize latitude = _latitude; @synthesize longitude = _longitude; -(void)dealloc{ self.locationManager = nil; [super dealloc]; } - (void)viewDidUnload { [super viewDidUnload]; self.locationManager = nil; } - (void)viewDidLoad { [super viewDidLoad]; // 实例化一个位置管理器 CLLocationManager *cllocationManager = [[CLLocationManager alloc] init]; self.locationManager = cllocationManager; [cllocationManager release]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; [self.locationManager startUpdatingLocation]; if(![CLLocationManager locationServicesEnabled]){ [GlobalApplication Alert:@"提示":@"请开启定位:设置 > 隐私 > 位置 > 定位服务"]; }else{ if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) { [GlobalApplication Alert:@"提示":@"定位失败,请开启定位:设置 > 隐私 > 位置 > 定位服务 下 XX应用"]; } } } #pragma mark - CLLocationManagerDelegate #pragma mark - CLLocationManagerDelegate // 地理位置发生改变时触发 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLLocationCoordinate2D cc,cc2; // 获取经纬度 cc.longitude = newLocation.coordinate.longitude; cc.latitude = newLocation.coordinate.latitude; // ios坐标(google)转换为 百度坐标 cc2 = BMKCoorDictionaryDecode(BMKBaiduCoorForWgs84(cc)); self.longitude = cc2.longitude; self.latitude = cc2.latitude; // 停止位置更新 [manager stopUpdatingLocation]; } // 定位失误时触发 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSString *errorString; [manager stopUpdatingLocation]; switch([error code]) { case kCLErrorDenied: errorString = @"定位失败,请开启定位:设置 > 隐私 > 位置 > 定位服务 下 XX应用"; //errorString = @"Access to Location Services denied by user"; break; case kCLErrorLocationUnknown: errorString = @"定位失败,位置数据不可用"; break; default: errorString = @"定位失败,未知错误"; break; } [GlobalApplication Alert:@"定位":errorString]; }
二、百度地图定位(版本号2.1)
1、MainMenuViewController.h
@interface MainMenuViewController : UIViewController<BMKUserLocationDelegate> { BMKUserLocation *_mapLocation; // 纬度 float _latitude; // 经度 float _longitude; } @property (nonatomic, retain) BMKUserLocation *mapLocation; @property (nonatomic) float latitude; @property (nonatomic) float longitude; @end
2、MainMenuViewController.m
#import "MainMenuViewController.h" @interface MainMenuViewController () @end @implementation MainMenuViewController @synthesize mapLocation = _mapLocation; @synthesize latitude = _latitude; @synthesize longitude = _longitude; -(void)dealloc{ self.mapLocation = nil; [super dealloc]; } - (void)viewDidUnload { [super viewDidUnload]; self.mapLocation = nil; } - (void)viewDidLoad { [super viewDidLoad]; BMKUserLocation *bmkLocation = [[BMKUserLocation alloc] init]; bmkLocation.delegate = self; self.mapLocation = bmkLocation; [bmkLocation release]; [self.mapLocation startUserLocationService]; } #pragma mark - baidu map /** *调用startUserLocationService定位成功后,会调用此函数 *@param userLoc 我的位置坐标 */ - (void)viewDidGetLocatingUser:(CLLocationCoordinate2D)userLoc{ if (userLoc.longitude != 0 && userLoc.latitude != 0 ) { self.longitude = userLoc.longitude; self.latitude = userLoc.latitude; [self.mapLocation stopUserLocationService]; } }
三、结果
方法一:
iOS:xxx.151604,xx.170156(iOS採集的坐标)
baidu:xxx.162720,xx.174000 (百度转换的坐标)
方法二:
baidu:xxx.162716,xx.173980 (坐标百度收藏)
iOS:xxx.151604,xx.170156(iOS採集的坐标)
baidu:xxx.162720,xx.174000 (百度转换的坐标)
方法二:
baidu:xxx.162716,xx.173980 (坐标百度收藏)
相关文章
- iOS 保持界面流畅的技巧
- iOS投屏搜索不到设备如何解决?投屏怎么设置?
- imazing23app最新Win与Mac系统的iOS移动设备管理软件
- iMazing2.14.6新版ios设备管理器支持越狱
- iMazing兼容Win和Mac2023免费版iOS设备管理器
- iMazing2.14.6Mac最新版iOS苹果设备管理器
- ios设备管理 iMazing官方免费激活电脑版下载v2.11.6.0
- IOS流媒体播放
- Android和iOS设备中的纹理压缩
- 快手回应董事长宿华“被带走配合调查”:恶意谣言,已报案;苹果关闭iOS 15.4.1验证通道;Linux 5.18发布|极客头条
- 9月20日科技资讯|余承东硬刚 iPhone11;苹果推送正式版 iOS 13;php-nsq 3.4.3 线上稳定版发布
- iOS - Runtime method_exchangeImplementations源码
- iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客
- 见过的最全的iOS面试题
- iOS开发 关于SEL的简单总结
- FFmpeg IOS 解封装
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的差别和联系
- 李洪强iOS开发之iOS好文章收集
- 李洪强漫谈iOS开发[C语言-038]-if else if语句
- 李洪强iOS开发之零基础学习iOS开发】【02-C语言】01-概述
- iOS开发 支付之银联支付集成
- iOS开发之集成讯飞听写
- IOS文件沙盒