iOS高德定位

  1. 要定位要先了解有关定位的几个协议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

相关文章
相关标签/搜索