工程配置
1、定位权限申请
(1) 需要在https://lbs.qq.com/dev/console/key/add上面申请一个apiKey,对应在TencentLBSLocationManager的apiKey属性上设置才可以使用。
(2) 需要在info.plist中追加 NSLocationWhenInUseUsageDescription 或NSLocationAlwaysUsageDescription 字段,以申请定位权限。
(3) allowsBackgroundLocationUpdates表示是否允许后台定位。默认为 NO。只在iOS 9.0 及以后起作用。设置为 YES 的时候必须保证 Background Modes 中的 Location updates 处于选中状态,否则会抛出异常。注意,如果不设置为YES,不需要申请该权限,否则会审核不通过!
2、引入定位包
将库文件 TencentLBS.framework 拷贝到您的APP的工程目录下,在Xcode的Target中选择 “Build Phases”->“Link Binary With Libraries”->“Add”->“Add Other…”-> 选择 TencentLBS.framework 目录并添加。
3、引入系统库文件
仿照上面的操作 ("Add"的时候直接在列表中查找选择 )添加系统库libz.1.2.5.tbd 在 Build Setting - Linking - Other Linker Flags 里 添加 -lstdc++.6.0.9, -lsqlite3(注意:在 Xcode9 以上,不再需要这两个库) 。
代码示例
- (void)configLocationManager
{
self.locationManager = [[TencentLBSLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setApiKey:@"对应于申请的apiKey "];
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
// 需要后台定位的话,可以设置此属性为YES。
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
// 如果需要POI信息的话,根据所需要的级别来设定,定位结果将会根据设定的POI级别来返回,如:
[self.locationManager setRequestLevel:TencentLBSRequestLevelName];
// 申请的定位权限,得和在info.list申请的权限对应才有效
CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestWhenInUseAuthorization];
}
}
// 单次定位
- (void)startSingleLocation {
[self.locationManager requestLocationWithCompletionBlock:
^(TencentLBSLocation *location, NSError *error) {
NSLog(@"%@, %@, %@", location.location, location.name, location.address);
}];
}
// 连续定位
- (void)startSerialLocation {
//开始定位
[self.locationManager startUpdatingLocation];
}
- (void)stopSerialLocation {
//停止定位
[self.locationManager stopUpdatingLocation];
}
- (void)tencentLBSLocationManager:(TencentLBSLocationManager *)manager
didFailWithError:(NSError *)error {
CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusDenied ||
authorizationStatus == kCLAuthorizationStatusRestricted) {
[self.displayLabel setText:@"定位权限没开启!"];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
message:@"定位权限未开启,是否开启?"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"是"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
if( [[UIApplication sharedApplication]canOpenURL:
[NSURL URLWithString:UIApplicationOpenSettingsURLString]] ) {
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"否"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alert animated:true completion:nil];
} else {
[self.displayLabel setText:[NSString stringWithFormat:@"%@", error]];
}
}
- (void)tencentLBSLocationManager:(TencentLBSLocationManager *)manager
didUpdateLocation:(TencentLBSLocation *)location {
//定位结果
NSLog(@"location:%@", location.location);
}
定位来源
typedef NS_ENUM(NSInteger, TencentLBSLocationProvider) {
TencentLBSLocationProviderUnkown = -1, //!< 普适定位结果来源未知
TencentLBSLocationProviderGPS = 0, //!< 普适定位结果来源手机的 GPS
TencentLBSLocationProviderNetWork = 1, //!< 普适定位结果来源手机的 Network
TencentLBSLocationProviderSimulated = 2, //!< 普适定位结果来源模拟定位
TencentLBSLocationProviderAccessoryGPS = 3, //!< 普适定位结果来源外设的 GPS
TencentLBSLocationProviderAccessoryNetwork = 4, //!< 普适定位结果来源外设的 Network
};
错误信息表示
typedef NS_ENUM(NSUInteger, TencentLBSLocationError) {
TencentLBSLocationErrorUnknown = 0, //!< 错误码,表示目前位置未知,但是会一直尝试获取
TencentLBSLocationErrorDenied = 1, //!< 错误码,表示定位权限被禁止
TencentLBSLocationErrorNetwork = 2, //!< 错误码,表示网络错误
TencentLBSLocationErrorHeadingFailure = 3, //!< 错误码,表示朝向无法确认
TencentLBSLocationErrorOther = 4, //!< 错误码,未知错误
};