简短代码实现原生定位
2017-05-17 16:44
309 查看
1、首先在info.plist文件中加入权限声明。请求用户获取定位能力
2、大体思路。
定位需要用户设备打开定位功能。这个可以根据这句代码判断。[CLLocationManager locationServicesEnables]如果为真则设备开启定位功能,否则没有开启。
判断用户是否为该应用设置允许定位可以根据CLLocationManagerDelegate的代理方法判断。具体见下面代码
2、遵循LocationManagerDelegate协议
3、定义属性。@property (nonatomic, strong) LocationManager *locationManager;
4、触发定位事件
5、在代理方法中拿到定位城市,更新UI
此外,想看完整demo的话,传送门项目地址
2、大体思路。
定位需要用户设备打开定位功能。这个可以根据这句代码判断。[CLLocationManager locationServicesEnables]如果为真则设备开启定位功能,否则没有开启。
判断用户是否为该应用设置允许定位可以根据CLLocationManagerDelegate的代理方法判断。具体见下面代码
#import <Foundation/Foundation.h> @class LocationManager; @protocol LocationManagerDelegate <NSObject> -(void)locationManager:(LocationManager *)locationManager didGotLocation:(NSString *)location; @end @interface LocationManager : NSObject @property (nonatomic, assign) id<LocationManagerDelegate> delegate; /** * 单例模式实例化对象 */ +(LocationManager *)sharedInstance; /** * 开始定位 */ -(void)autoLocate; @end #import "LocationManager.h" #import <CoreLocation/CoreLocation.h> @interface LocationManager()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @end @implementation LocationManager +(LocationManager *)sharedInstance{ static LocationManager *instance = nil; static dispatch_once_t predict; dispatch_once(&predict, ^{ instance = [[self alloc] init]; }); return instance; } #pragma mark - private method -(void)autoLocate{ if ([CLLocationManager locationServicesEnabled]) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; [self.locationManager startUpdatingLocation]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){ [self.locationManager requestWhenInUseAuthorization]; } } } #pragma mark - CLLocationManagerDelegate -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"打开定位开关" message:@"定位服务未开启,请进入系统【设置】>【隐私】>【定位服务】中打开开关,并允许母子健康手册使用定位服务" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //打开该App的权限设置 NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:settingsURL]; }]; UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:cancel]; [alertVC addAction:ok]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil]; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ [manager stopUpdatingLocation]; CLLocation *currentLocation = [locations lastObject]; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *place = placemarks[0]; if (self.delegate && [self.delegate respondsToSelector:@selector(locationManager:didGotLocation:)]) { [self.delegate locationManager:self didGotLocation:place.locality]; } }]; } @end
How to use
1、在你的文件中导入#import “LocationManager.h”2、遵循LocationManagerDelegate协议
3、定义属性。@property (nonatomic, strong) LocationManager *locationManager;
4、触发定位事件
//自动定位 -(void)autoLocate{ self.locationManager = [LocationManager sharedInstance]; self.locationManager.delegate = self; [self.locationManager autoLocate]; }
5、在代理方法中拿到定位城市,更新UI
#pragma mark - LocationManagerDelegate -(void)locationManager:(LocationManager *)locationManager didGotLocation:(NSString *)location{ self.conditionView.cityName = location; }
此外,想看完整demo的话,传送门项目地址
相关文章推荐
- 简短代码实现原生定位
- 一款双向无缝+按钮定位的焦点图实现代码
- OpenCV下车牌定位算法实现代码(一)
- OpenCV下车牌定位算法实现代码
- 使用FUNCNAME 和BASH_LINENO实现shell脚本中定位函数错误在代码中的位置
- 原生js 秒表实现代码
- 纯C代码实现重定位
- jQuery选项卡的实现(代码简短的)
- jquery依然简短几句代码实现给元素动态添加及获取提示信息
- jquery 简短几句代码实现给元素动态添加及获取提示信息
- 【代码片段】jQuery实现页面滚动时层智能浮动定位-Fixed Floating Elements
- 一列固定宽度布局和背景图片绝对定位的实现代码
- JavaScript与Div对层定位和移动获得坐标的实现代码
- 原生javascript实现图片轮播效果代码
- 简短几句jquery代码的实现一个图片向上滚动切换
- 可以自己实现重定位的代码
- JavaScript 浮动定位提示效果实现代码第1/2页
- OpenCV下车牌定位算法实现代码
- C# 播放铃声最简短的代码实现方式
- 固定浮动定位(fixed)实现思路及代码