本次需求只需要定位到当前位置,拿到粗略的经纬度即可,所以弃用百度地图,直接用苹果自带的CLLocatioManager即可实现。
一、CLLocationManager获取定位
- 1、在info.plist文件里添加 这两个描述 获得用户的允许权限
Privacy - Location When In Use Usage Description -> 是否允许此App在使用期间访问你的位置?
97F7568B-615B-429D-A248-AC56699E10CD.pngPrivacy - Location Always Usage Description -> 是否允许此App永久访问你的位置?
-
2、添加库 CoreLocation.framework
BFCC7972-B36B-45FE-AA63-2528943C6D95.png -
3、下面就是大家最喜欢的部分了- 开始码砖
可以直接在需要的VC中直接写CLLocationManager,也可以分出来写个manager类,这样项目中可以复用
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
typedef void (^locationInfoBlock) (CLPlacemark *mark, CLLocation *location);
typedef void (^coordinate2DBlock) (NSNumber *latitude, NSNumber *longitude);
@interface SmartCLLocationManager : NSObject
+ (void)startLocation:(locationInfoBlock)location;
@end
#import "SmartCLLocationManager.h"
#import "SmartToast.h"
#import "Util.h"
@interface SmartCLLocationManager ()<CLLocationManagerDelegate>
{
CLLocationManager *_locationmanager;
locationInfoBlock locationBlock;
}
@end
@implementation SmartCLLocationManager
+(instancetype)shareManager
{
static SmartCLLocationManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[SmartCLLocationManager alloc] init];
});
return manager;
}
-(instancetype)init
{
if (self = [super init]) {
[self initLocationManager];
}
return self;
}
+ (void)startLocation:(locationInfoBlock)location
{
[[SmartCLLocationManager shareManager] startLocation:location];
}
- (void)startLocation:(locationInfoBlock)location
{
if (![CLLocationManager locationServicesEnabled]) {
[self showAlert];
return;
}
locationBlock = [location copy];
[_locationmanager stopUpdatingLocation];//停止上次定位
[_locationmanager startUpdatingLocation];//开始新的定位
}
- (void)initLocationManager
{
if ([CLLocationManager locationServicesEnabled]) {
_locationmanager = [[CLLocationManager alloc] init];
_locationmanager.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0) {
[_locationmanager requestWhenInUseAuthorization];//前台定位
// [_locationmanager requestAlwaysAuthorization];//前后台同时定位
}
_locationmanager.desiredAccuracy = kCLLocationAccuracyKilometer;//kCLLocationAccuracyBest比较耗性能
_locationmanager.distanceFilter = 5.0;
}else{
[self showAlert];
}
}
#pragma mark --- CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations//定位成功
{
CLLocation *currentLocation = [locations lastObject];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
//打印当前的经度与纬度
DLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
//反地理编码
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
NSString *currentCity = placeMark.locality;
if (locationBlock) {
locationBlock(placeMark,currentLocation);
}
/*看需求定义一个全局变量来接收赋值*/
DLog(@"----%@",placeMark.country);//当前国家
DLog(@"%@",currentCity);//当前的城市
DLog(@"%@",placeMark.subLocality);//当前的位置
DLog(@"%@",placeMark.thoroughfare);//当前街道
DLog(@"%@",placeMark.name);//具体地址
}else if (error == nil && placemarks.count == 0){
DLog(@"没有定位信息");
}else if (error != nil){
DLog(@"error");
}
}];
[_locationmanager stopUpdatingHeading];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error//定位失败
{
if (locationBlock){
locationBlock(nil,nil);
}
[_locationmanager stopUpdatingLocation];
if ([error code] == kCLErrorDenied) {//没有授权
[self showAlert];
}
else if ([error code] == kCLErrorLocationUnknown){
[SmartToast Tost:@"定位数据不可用"];
}
else{
[SmartToast Tost:@"未知错误,定位失败"];
}
}
-(void)showAlert
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允许定位提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[Util jumpToWifiSetting];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:okAction];
[alert addAction:cancelAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
二、根据位置获取经纬度
+ (void)converAdress:(NSString *)address ToCoordinate2D:(coordinate2DBlock)coordinate;//根据传入的地理位置-->经纬度
+ (void)converAdress:(NSString *)address ToCoordinate2D:(coordinate2DBlock)coordinate//根据传入的地理位置-->经纬度
{
CLGeocoder *ge = [[CLGeocoder alloc] init];
[ge geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if ([placemarks count] > 0 && error == nil) {
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
NSNumber *latitude = [NSNumber numberWithDouble:firstPlacemark.location.coordinate.latitude];
NSNumber *longitude = [NSNumber numberWithDouble:firstPlacemark.location.coordinate.longitude];
coordinate(latitude,longitude);
}else if ([placemarks count] == 0 && error == nil) {
DLog(@"Found no placemarks.");
coordinate(nil,nil);
}else if (error != nil) {
DLog(@"An error occurred = %@", error);
coordinate(nil,nil);
}
}];
}
本次就简单介绍一下系统定位,后面再补充上百度地图。
网友评论