簡短代碼實現原生定位

一、首先在info.plist文件中加入權限聲明。請求用戶獲取定位能力 輸入圖片說明git

二、大致思路。github

定位須要用戶設備打開定位功能。這個能夠根據這句代碼判斷。[CLLocationManager locationServicesEnables]若是爲真則設備開啓定位功能,不然沒有開啓。 判斷用戶是否爲該應用設置容許定位能夠根據CLLocationManagerDelegate的代理方法判斷。具體見下面代碼atom

#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###spa

一、在你的文件中導入#import "LocationManager.h" 二、遵循LocationManagerDelegate協議<LocationManagerDelegate> 三、定義屬性。@property (nonatomic, strong) LocationManager *locationManager; 四、觸發定位事件.net

//自動定位
-(void)autoLocate{
self.locationManager = [LocationManager sharedInstance];
self.locationManager.delegate = self;
[self.locationManager autoLocate];
}

五、在代理方法中拿到定位城市,更新UI代理

#pragma mark - LocationManagerDelegate
-(void)locationManager:(LocationManager *)locationManager didGotLocation:(NSString *)location{
self.conditionView.cityName = location;
}

此外,想看完整demo的話,傳送門項目地址code

相關文章
相關標籤/搜索