1、注:info.plist里面加NSLocationWhenInUseUsageDescription(string)类型
iOS10之后在plist里面加:
定位权限:Privacy - Location When In Use Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
定位权限: Privacy - Location Always Usage Description 我们需要通过您的地理位置信息获取您周边的相关数据
定位的需要这么写,防止上架被拒。
2、添加Framework
#import <CoreLocation/CoreLocation.h>
3、添加代理,属性
<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManage;
@property (nonatomic, assign) CLLocationCoordinate2D locationCoordinate2D;
@property (nonatomic, strong) CLGeocoder *geocoder;
4、GelocationProvider.m文件添加block回调地址给控制器
@property (nonatomic, copy) void (^gelocationBlock) (NSString *string);
4、GelocationProvider.h文件代码
- (instancetype)init
{
if (self = [super init]) {
self.locationManage.delegate = self;
self.locationCoordinate2D = CLLocationCoordinate2DMake(55, 34);
if ([[UIDevice currentDevice].systemVersion intValue] <= 7) {
NSLog(@"%.2f", [[UIDevice currentDevice].systemVersion doubleValue]);
}
else {
NSLog(@"%.2f", [[UIDevice currentDevice].systemVersion doubleValue]);
[self.locationManage requestWhenInUseAuthorization];
}
[self.locationManage startUpdatingLocation];
//需要等待进行locationManage走代理方法
[self performSelector:@selector(getLocationInfo) withObject:nil afterDelay:1.0f];
//反地理编码获取位置信息
}
return self;
}
- (void)getLocationInfo
{
CLLocation *location = [[CLLocation alloc] initWithLatitude:self.locationCoordinate2D.latitude longitude:self.locationCoordinate2D.longitude];
NSLog(@"latitude->%f, longitude->%f", self.locationCoordinate2D.latitude, self.locationCoordinate2D.longitude);
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error || placemarks.count == 0) {
self.gelocationBlock(error.localizedDescription);
}
else {
CLPlacemark *placemark = [placemarks firstObject];
NSLog(@"-->%@", placemark.name);
self.gelocationBlock(placemark.name);
}
}];
}
- (CLGeocoder *)geocoder
{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
- (CLLocationManager *)locationManage
{
if (!_locationManage) {
_locationManage = [[CLLocationManager alloc] init];
}
return _locationManage;
}
- (void)setLocationCoordinate2D:(CLLocationCoordinate2D)locationCoordinate2D
{
_locationCoordinate2D = locationCoordinate2D;
}
#pragma mark --CLLocationManageDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error->%@", error.localizedDescription);
[self.locationManage stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *location = [locations lastObject];
self.locationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[self.locationManage stopUpdatingLocation];
}
6、在ViewController里面获取位置信息
GelocationProvider *gelocation = [[GelocationProvider alloc] init];
gelocation.gelocationBlock = ^(NSString *string) {
NSLog(@"位置信息->%@", string);
}