第一,我们要导入苹果自带的库
#import <MapKit/MapKit.h>
第二,我们要创建对象
@property (strong,nonatomic) CLLocationManager* locationManager;
第三,我们要遵守协议
@interface LocationViewController :UIViewController<CLLocationManagerDelegate>
第四,我们要配置Info.Plist,添加一下两个字段,
Privacy - Location When In Use Usage Description 后面为字符串类型(随意填写) 例如:请您允许,我们将会为您提供更精确的信息
Privacy - Location Always Usage Description 后面为字符串类型(随意填写)例如:请您允许,我们将会为您提供更精确的信息 // 特别声明一下,这个字段的添加要看你的需求,这个功能是指在后台时也可以时时定位,所以app如果不需要的话,不建议添加,否则上线审核会出现问题,如果需要需要做一些声明描述:GPS在后台持续运行,可以大大降低电池的寿命。
效果图如下:
第五,接下来就是大家最喜欢的粘代码
//开始定位
-(void)startLocation{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
if ([[[UIDevice currentDevice]systemVersion]doubleValue] >8.0){
[self.locationManager requestWhenInUseAuthorization];
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
_locationManager.allowsBackgroundLocationUpdates =YES;
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
break;
default:
break;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *newLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
[manager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark *placein placemarks) {
NSLog(@"name,%@",place.name); // 位置名
NSLog(@"thoroughfare,%@",place.thoroughfare); // 街道
NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道
NSLog(@"locality,%@",place.locality); // 市 NSLog(@"subLocality,%@",place.subLocality); // 区
NSLog(@"country,%@",place.country); // 国家
if ([JudgeIDAndBankCardisEmptyOrNull:_gpsCityName]) {
_gpsCityName=@"定位失败";
}
}
}];
}
这样我们就能定位到当前所在城市以及经纬度,在上面的方法可以获取
第六,如果还是获取不到,看看你是不是在模拟器上运行的
第七,如果还是没有解决按照以下图片进行操作到 Capabilities ——>找到Background Modes 将off改成on 并且勾选 Location updates
这样我们就能定位到当前所在城市以及经纬度,在上面的方法可以获取
到此获取经纬度以及城市名称就完成了
既然用到了定位,那么就有可能用到判断手机给了APP定位权限问题,当然判断着个也要引库和遵守协议
#import <CoreLocation/CLLocationManager.h>
CLLocationManagerDelegate
下面就是判断是否打开了定位权限。返回YES代表没打开,我们需要提使用用户
+ (BOOL)openLocationServiceWithBlock {
if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
return NO;
} else {
return YES;
}
}
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"alert_title", @"提示") message:NSLocalizedString(@"Opne_location_serive", @"定位服务未开启,请进入系统[设置]> [隐私] > [定位服务]中打开开关,并允许使用定位服务。") delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", @"取消") otherButtonTitles:NSLocalizedString(@"SetUp", @"设置"), nil];
[alertView show];
点击确定按钮我们自动跳转到设置页面去设置即可
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ( buttonIndex == 1 ) {
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}
}