要定位要先了解有關定位的幾個協議git
<CLLocationManagerDelegate,MKMapViewDelegate>測試
2.包含兩個頭文件<MapKit/MapKit.h> <CoreLocation/CoreLocation.h>spa
2.code
#pragma mark - CLLocationDelegate //在座標改變的時候纔會調用 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
#pragma mark - MKMapDelegate //每次定位都會調用 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
3.但iOS8以後有時候定位會不成功orm
須要要在info.plist加一個文件就不會有這種狀況了 -- 》NSLocationAlwaysUsageDescriptionip
4.定位測試的時候最好用真機,模擬機常常不行的。ci
5.如今考慮兩個問題,如何提醒用戶開啓定位?如何提醒用戶開啓定位受權(光是開啓定位是沒用的,還須要給予定位的權限)get
5.1提醒用戶開啓定位能夠寫在it
if (![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) { [_locationManager requestWhenInUseAuthorization]; }else { UIAlertController * alterC = [UIAlertController alertControllerWithTitle:@"請打開定位" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alterC addAction:[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDestructive handler:nil]]; [self presentViewController:alterC animated:YES completion:nil]; }
5.2 而用戶定位權限的提醒不開的話,2 裏面寫的兩個協議裏面的方法就不會調用了,那如何知道用戶沒有開啓定位權限?io
我是採用了一個時間定時器。
這只是我我的的思路 若是你們有更好的能夠提意見。
思路1:由於若是說當經緯度爲0 的時候就跳出一個提醒框,會有一個問題,必須設一個全局變量的經緯度,而後用於接收協議方法裏的到的金緯度,若是金緯度爲0,0就跳出提醒,不爲0,0就不跳出,但是剛開始要先初始化這兩個全局的經緯度,因此剛開始確定是先爲(0,0)纔不爲(0,0),因此無論給沒給APP定位權限都會跳出提醒框,因此這個思路不行。
思路2:結合以上思路在加一個時間控制器,若是經緯度爲(0,0)而且時間控制器不斷的調用一個方法,這個方法裏面加了一個參數 _value,沒次都給他加一個1,設置大概時間爲8秒,若是8秒後經緯度仍是爲(0,0)而_value的值已經大於8了,那就說明用戶沒有給予APP定位權限這個時候就能夠跳提醒。
{ NSInteger _value; NSTimer * _timer; } 先給方法傳入一個經緯度爲0,0的值 [self cityLatitude:0 longtitude:0]; //時間控制器 _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(cityLatitude:longtitude:) userInfo:nil repeats:YES]; //提醒打開設置--隱私--定位 - (void)cityLatitude:(CLLocationDegrees)latitiude longtitude:(CLLocationDegrees)longtitude { _value++; // NSLog(@"%f %f %ld",latitiude,longtitude,(long)_value); if (_value > 8) { [_timer invalidate]; }else if (latitude != 0) { [_timer invalidate]; } if (_value >8 && latitiude == 0) { UIAlertController * alterC = [UIAlertController alertControllerWithTitle:@"請打開定位" message:@"設置->隱私->定位->打開" preferredStyle:UIAlertControllerStyleAlert]; [alterC addAction:[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDestructive handler:nil]]; [self presentViewController:alterC animated:YES completion:nil]; } }
#pragma mark - MKMapDelegate -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { //把定位經緯度發送給 根據座標算出城市名的方法 [self getCityNameByLatitude:userLocation.location.coordinate.latitude longtitude:userLocation.location.coordinate.longitude]; //吧座標發個 判判定位是否受權的方法 [self cityLatitude:userLocation.location.coordinate.latitude longtitude:userLocation.location.coordinate.longitude]; }
根據經緯度算出用戶所在城市,而經緯度就是協議方法裏的獲得的
- (void)getCityNameByLatitude:(CLLocationDegrees)latitude longtitude:(CLLocationDegrees)longtitude { CLLocation * location = [[CLLocation alloc]initWithLatitude:latitude longitude:longtitude]; [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark * placeMark = [placemarks firstObject]; NSLog(@"%@",[placeMark.addressDictionary objectForKey:@"City"]); _cityModel.cityName = [placeMark.addressDictionary objectForKey:@"City"]; [_button setTitle:_cityModel.cityName forState:UIControlStateNormal]; }]; [_locationManager stopUpdatingLocation]; }
Demo:http://pan.baidu.com/s/1hrdWx3Q