iOS定位和地图

2018-07-03  本文已影响39人  宙斯YY

一.定位

1.iOS8以后前台定位

A.代码

        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [_locationManager requestAlwaysAuthorization];
        _locationManager.distanceFilter = 10.0f;
        if(@available(iOS 8.0,*)) {
            [_locationManager requestWhenInUseAuthorization];
        }

B.配置信息Info.plist

WX20180703-153503.png
或者使用这个NSLocationWhenInUseUsageDescription作为KEY
2.iOS8以后后台定位

A.代码

    if(_locationManager==nil)
    {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [_locationManager requestAlwaysAuthorization];
        _locationManager.distanceFilter = 10.0f;
        if(@available(iOS 8.0,*)) {
            [_locationManager requestAlwaysAuthorization];
        }
        //iOS9以后需要添加
        if (@available(iOS 9.0, *)) {
            _locationManager.allowsBackgroundLocationUpdates=YES;
        }
    }

B.配置信息Info.plist

WX20180703-154013.png
或者使用这个NSLocationAlwaysAndWhenInUseUsageDescription作为KEY

C.APP能力权限配置


WX20180703-154232.png
3.获取定位服务状态信息
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if(self.statusblock)
    {
        switch (status) {
            case kCLAuthorizationStatusNotDetermined:
                SDLog(@"用户没选择");
                self.statusblock(StatusType_UserDontChoose);
                break;
            case kCLAuthorizationStatusRestricted:
                SDLog(@"被限制");
                self.statusblock(StatusType_UserRestricted);
                break;
            case kCLAuthorizationStatusDenied:
                SDLog(@"用户拒绝");
                self.statusblock(StatusType_UserDenied);
                break;
            case kCLAuthorizationStatusAuthorizedAlways:
                SDLog(@"用户选择后台定位");
                self.statusblock(StatusType_UserAgreeAlways);
                break;
            case kCLAuthorizationStatusAuthorizedWhenInUse:
                SDLog(@"用户选择前台定位");
                self.statusblock(StatusType_UserAgreeWhenInUse);
                break;
            default:
                break;
        }
    }
}
4.CLLocationManager
[self.locationManager startUpdatingLocation];

#pragma mark - 定位信息回调方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations 

CLLocation类说明

//CLLocationCoordinate2D包含经纬度信息结构体
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
struct CLLocationCoordinate2D {
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
};
//海拔信息
@property(readonly, nonatomic) CLLocationDistance altitude;
//水平精度
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
//垂直精度
@property(readonly, nonatomic) CLLocationAccuracy verticalAccuracy;
//方向(range:0.0 - 359.9 degrees, 0 being true North)
@property(readonly, nonatomic) CLLocationDirection course;
//速率
@property(readonly, nonatomic) CLLocationSpeed speed;
//获取两个CLLocation之间距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;
if([CLLocationManager headingAvailable])
{
[self.locationManager startUpdatingHeading];
}

#pragma mark - 设备方向信息回调
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading

CLHeading类说明

//地磁方向( Range:0.0 - 359.9 degrees, 0 being magnetic North)
@property(readonly, nonatomic) CLLocationDirection magneticHeading;
//真实方向( Range:0.0 - 359.9 degrees, 0 being magnetic North)
@property(readonly, nonatomic) CLLocationDirection trueHeading;
[self.locationManager startMonitoringForRegion:region];
[self.locationManager requestStateForRegion:region];

#pragma mark - 区域信息回调
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region

CLRegionState,CLRegion类说明

typedef NS_ENUM(NSInteger, CLRegionState) {
    CLRegionStateUnknown,
    CLRegionStateInside,
    CLRegionStateOutside
} NS_ENUM_AVAILABLE(10_10, 7_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
//CLCircularRegion圆形区域
 CLCircularRegion * region =[CLCircularRegion alloc]initWithCenter:<#(CLLocationCoordinate2D)#> radius:<#(CLLocationDistance)#> identifier:<#(nonnull NSString *)#>
上一篇 下一篇

猜你喜欢

热点阅读