美文网首页
iOS定位和地图

iOS定位和地图

作者: 宙斯YY | 来源:发表于2018-07-03 15:37 被阅读39次

一.定位

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 *)#>

相关文章

  • IOS地图定位导航

    title : IOS地图定位导航category : UI 地图定位导航 标签(空格分隔): IOS 概述 I...

  • 定位和地图

    为了使用iOS中的地图和定位功能,我们需要使用Core Location和Map Kit,分别用于地理定位和地图展...

  • iOS定位和地图

    一.定位 1.iOS8以后前台定位 A.代码 B.配置信息Info.plist 2.iOS8以后后台定位 A.代码...

  • IOS 定位和地图

    定位 //第一步导入框架 @import CoreLocation; //协议和属性 @interface Loc...

  • IOS定位和地图

    现在手机软件基本上都需要定位,比如打个的,附近的人,附近的餐厅等等,这些应用都需要定位和地图服务。 我这个人分不清...

  • iOS地图和定位

    iOS地图定位 本文发布在http://he8090.cn/2016/07/18/地图与定位/ 导入地图框架 1、...

  • iOS 定位和地图

    简介: 在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆、找KTV、找电影院等等 导航:根据...

  • 地图与定位

    OCiOS开发:地图与定位 - 李鴻耀 - 博客频道 - CSDN.NET iOS开发之地图-定位/...

  • 定位及耗电

    iOS-GPS定位基础知识 iOS -GPS定位服务和地图应用是两套完全不同的API iOS7 的四种定位服务-G...

  • iOS Mapkit的使用

    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等 标签:iOS地图mapkit 1.显示地图 (1)首...

网友评论

      本文标题:iOS定位和地图

      本文链接:https://www.haomeiwen.com/subject/rvxjuftx.html