Skip to content

位置服务

提供和用户位置相关的能力服务

添加依赖库

  • 将解压后的DMKLocationKit.framework 文件copy或拖拽到工程文件夹中
  • 左侧目录中选中工程名,到 TARGETS->Build Phases-> Link Binary With Libaries 菜单,将DMKLocationKit.framework拖拽进这个菜单
  • TARGETS->Build Settings->Other Linker Flags 需要添加参数 -ObjC

1 持续定位

根据使用方配置的参数返回符合使用方要求的系统定位点 代码示例

#import <DMKLocationKit/DMKLocationAdapter.h>
- (void)startLocation {
    //step 1 配置自己需要的定位参数
    DMKLocationConfig *locationConfig = [[DMKLocationConfig alloc] init];locationConfig.needNavMode = YES;
    locationConfig.distanceFilter = 0.0;
    locationConfig.needBackgroundUpdate = YES;
    
    //step2 注册监听
    DMKLocationAdapter *locationAdapter = [DMKLocationAdapter sharedInstance];
    [locationAdapter registerWithDelegate:self locationConfig:locationConfig error:NULL];
}

//step3 实现代理
- (void)locationManagerDidUpdateLocation:(DMKLocation *)location {
    // 添加获取定位点后的实现

}

//step4 不再需要定位时反注册定位监听
- (void)stopLocation {
     [[DMKLocationAdapter sharedInstance] unregisterWithDelegate:self error:NULL];
}

2 单次定位

异步获取一次系统定位点 代码示例

#import <DMKLocationKit/DMKSharedSingleClient.h>

- (void)getLocationOnce {
    DMKSharedSingleClient *singleClient = [DMKSharedSingleClient sharedInstance];
    [singleClient requestLocationWithCompletionBlock:^(DMKLocation * _Nullable location, NSError * _Nullable error) {
            //添加获取定位点后的实现
    }];
}

3 获取当前位置

获取最新位置

代码示例

#import <DMKLocationKit/DMKSharedSingleClient.h>

- (void)getCurrentLocation {
    DMKLocationAdapter *locationAdapter = [DMKLocationAdapter sharedInstance];
    DMKLocation *currentLocation = locationAdapter.currentLocation;
    //添加最新位置使用代码
}

4 智能定位

智能定位是指结合系统定位点 和 加速计、 陀螺仪、以及路网信息、综合得出的连续性更好、精度更高的定位点

添加Motion使用说明 在App的权限使用说明中添加Motion使用说明 Privacy - Motion Usage Description

代码示例

//step1 导入头文件
#import <DMKLocationKit/DMKFLPAdapter.h>

- (void)startFLPLocation {
    //step2 注册监听
    DMKFLPAdapter *adapter = [DMKFLPAdapter mainAdapter];
    [adapter startFLPUpdatingLocationWithDelegate:self];
}

//step3 实现代理
- (void)flpAdapter:(DMKFLPAdapter *)adapter didUpdateLocation:(DMKFLPLocationModel *)flpLocationModel { 
    // 智能定位点使用代码

}

// step3 取消监听
- (void)stopFLPLocation {
    DMKFLPAdapter *adapter = [DMKFLPAdapter mainAdapter];
    [adapter updateNaviStop:1];
    [adapter stopFLPUpdatingLocationWithDelegate:self];
}

5 逆地理

基于用户的位置获取用户所在的城市信息,或者是否境内境外 代码示例

#import <DMKLocationKit/DMKLocationRGeoAdapter.h>

//用户位置是否在境外
- (void)isUserAbroad {
     BOOL isAbroad = [[DMKLocationRGeoAdapter sharedInstance] currentCityModel].isAbroad;
     return isAbroad;
}