• 凉风有兴,秋月无边, 亏我思娇的情绪好比度日如年。
  • 虽然我不是玉树临风,潇洒倜傥, 可是我有我广阔的胸襟,加强健的臂腕!

获取定位信息

网站推广 villain 7个月前 (01-07) 61次浏览 已收录 0个评论

先导入

获取定位信息

然后在 info.plist 文件中添加 Privacy – Location Always and When In Use Usage Description 和 Privacy – Location When In Use Usage Description 两个就可以了。

LocationManager.h

#import Foundation/Foundation.h 
typedef void(^LocationBlock)(NSString *lat, NSString *lon);
@interface LocationManager : NSObject
+ (instancetype)shareManager;
- (void)getGps:(LocationBlock)block;
@property (nonatomic, copy) NSString *lat;
@property (nonatomic, copy) NSString *lon;
@end

LocationManager.m

#import "LocationManager.h"
#import CoreLocation/CoreLocation.h 
@interface LocationManager () CLLocationManagerDelegate 
@property (nonatomic, strong) CLLocationManager *locManager;
@property (nonatomic, copy) LocationBlock block;
@implementation LocationManager
+ (instancetype)shareManager {
 static LocationManager *_manager;
 static dispatch_once_t onceToken;
 dispatch_once( onceToken, ^{
 _manager = [[LocationManager alloc] init];
 return _manager;
- (instancetype)init {
 if (self = [super init]) {
 _locManager = [[CLLocationManager alloc] init];
 // 精确度
 [_locManager setDesiredAccuracy:kCLLocationAccuracyBest];
 // 精确到米
 _locManager.distanceFilter = 100;
 _locManager.delegate = self;
 if (![CLLocationManager locationServicesEnabled]) { // 判断当前服务是否开启
 NSLog(@"开启定位服务");
 } else {
 // 当前状态
 CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
 if (status == kCLAuthorizationStatusNotDetermined) { // 未选中状态时候
 [_locManager requestWhenInUseAuthorization];
 return self;

– (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray CLLocation * *)locations {


CLLocation *newLocation = locations[0];


// 获取当前所在的城市名

CLGeocoder *geocoder = [[CLGeocoder alloc] init];


// 根据经纬度反向地理编译出地址信息

[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){


if (array.count 0){


CLPlacemark *placemark = [array objectAtIndex:0];

// 获取城市

NSString *city = placemark.locality;


if (!city) {


// 四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

city = placemark.administrativeArea;

}


NSLog(@”city = %@”, city); // 广州市

NSLog(@”–%@”,placemark.name);// 黄埔大道西447-5号

NSLog(@”++++%@”,placemark.subLocality); // 天河区

NSLog(@”country == %@”,placemark.country);// 中国

NSLog(@”administrativeArea == %@”,placemark.administrativeArea); // 广东省

}

    else if (error == nil [array count] == 0)

{

NSLog(@”No results were returned.”);

}

else if (error != nil)

{

NSLog(@”An error occurred = %@”, error);

}

}];


CLLocationCoordinate2D coor = newLocation.coordinate;

NSString *lat = [NSString stringWithFormat:@”%@”, @(coor.latitude)];

NSString *lon = [NSString stringWithFormat:@”%@”, @(coor.longitude)];


[LocationManager shareManager].lat = lat;

[LocationManager shareManager].lon = lon;

self.block(lat, lon);


// 系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新

[self.locManager stopUpdatingLocation];

}

- (void)getGps:(LocationBlock)block {
 self.block = block;
 // 开始定位
 [self.locManager startUpdatingLocation];
@end

调用方法:

[[LocationManager shareManager] getGps:^(NSString *lat, NSString *lon) {
 NSLog(@"%@, %@", lat, lon);
 }];


Villain博客 , 版权所有丨如有问题请联系客服QQ:1004619丨
转载请注明获取定位信息
喜欢 (0)
[gqakcom@126.com]
分享 (0)

您必须 登录 才能发表评论!