Files
tianrunCRM/Assets/3rd/LBSBaidu/Plugins/iOS/SingleLocPlugin.m
2020-07-04 14:41:25 +08:00

155 lines
5.3 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SingleLocDemoViewController.m
// IphoneMapSdkDemo
//
// Created by baidu on 2017/4/5.
// Copyright © 2017年 Baidu. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SingleLocPlugin.h"
static SingleLocPlugin* _singleLocPlugin;
@implementation SingleLocPlugin
+(SingleLocPlugin*) instance
{
if (_singleLocPlugin != nil) {
return _singleLocPlugin;
} else {
_singleLocPlugin =(SingleLocPlugin*)([[SingleLocPlugin alloc] init]);
}
return _singleLocPlugin;
}
- (id)init:(NSString *)ak withUnityListner:(NSString *)listner andCoorType:(int)coordinateType
{
unityListner = listner;
[[BMKLocationAuth sharedInstance] checkPermisionWithKey:ak authDelegate:self];
[self initLocation:coordinateType];
return self;
}
-(void)initLocation:(int)coordinateType
{
_locationManager = [[BMKLocationManager alloc] init];
_locationManager.delegate = self;
if(coordinateType == 0) {
_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
} else if(coordinateType == 1) {
_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09MC;
} else if(coordinateType == 2) {
_locationManager.coordinateType = BMKLocationCoordinateTypeWGS84;
} else {
_locationManager.coordinateType = BMKLocationCoordinateTypeGCJ02;
}
_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL; //BMKLocationCoordinateTypeGCJ02
_locationManager.distanceFilter = [distance.text doubleValue];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
_locationManager.pausesLocationUpdatesAutomatically = NO;
_locationManager.allowsBackgroundLocationUpdates = NO;// YES的话是可以进行后台定位的但需要项目配置否则会报错具体参考开发文档
_locationManager.locationTimeout = 10;
_locationManager.reGeocodeTimeout = 10;
}
-(void)getMyLocation
{
self.completionBlock = ^(BMKLocation *location, BMKLocationNetworkState state, NSError *error)
{
long code = 0
NSString *msg = nil;
float latitude = 0;
float longitude = 0;
NSString *AddrStr = @"";
if (error)
{
code = (long)error.code;
msg = error.localizedDescription;
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
}
if (location.location) {//得到定位信息添加annotation
NSLog(@"LOC = %@",location.location);
NSLog(@"LOC ID= %@",location.locationID);
latitude = location.location.coordinate.latitude;
longitude = location.location.coordinate.longitude;
if(location.rgcData) {
AddrStr = [location.rgcData description];
}
NSLog([NSString stringWithFormat:@"当前位置信息: \n经纬度:%.6f,%.6f \n地址信息:%@ \n网络状态:%d",location.location.coordinate.latitude, location.location.coordinate.longitude, AddrStr, state]);
}
NSLog(@"netstate = %d",state);
NSString * json = [NSString stringWithFormat:@"{\"cmd\":\"onGetLocation\", \"code\":%ld,\"msg\":\"%@\",\"latitude\":%0.6f,\"longitude\":%0.6f,\"AddrStr\":\"%@\"}",code, msg, latitude, longitude, AddrStr];
[self sendUnityMessage:@"onCallback", json]
};
}
-(void) sendUnityMessage:(const NSString*)method with:(NSString*)msg
{
if(unityListner != nil && msg != nil) {
UnitySendMessage([unityListner UTF8String],
[method UTF8String], [msg UTF8String]);
}
}
- (void)onGetPermissionState:(int)iError
{
if (0 == iError) {
NSLog(@"授权成功");
}
else {
NSLog(@"onGetPermissionState %d",iError);
}
}
- (void)onCheckPermissionState:(BMKLocationAuthErrorCode)iError
{
NSLog(@"location auth onGetPermissionState %ld",(long)iError);
}
/**
* @brief 为了适配app store关于新的后台定位的审核机制app store要求如果开发者只配置了使用期间定位则代码中不能出现申请后台定位的逻辑当开发者在plist配置NSLocationAlwaysUsageDescription或者NSLocationAlwaysAndWhenInUseUsageDescription时需要在该delegate中调用后台定位api[locationManager requestAlwaysAuthorization]。开发者如果只配置了NSLocationWhenInUseUsageDescription且只有使用期间的定位需求则无需在delegate中实现逻辑。
* @param manager 定位 BMKLocationManager 类。
* @param locationManager 系统 CLLocationManager 类 。
* @since 1.6.0
*/
- (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager doRequestAlwaysAuthorization:(CLLocationManager * _Nonnull)locationManager
{
[locationManager requestAlwaysAuthorization];
}
- (void)dealloc {
_locationManager = nil;
_completionBlock = nil;
}
@end
#if __cplusplus
extern "C" {
#endif
void _init(const char* ak, const char* goName, const int coorType);
void _getMyLocation();
#if __cplusplus
}
#endif
void _init(const char* ak, const char* goName, const int coorType); {
[[SingleLocPlugin instance] init:[[NSString stringWithUTF8String:ak] withUnityListner:[NSString stringWithUTF8String:goName andCoorType:coorType] retain]];
}
void _getMyLocation() {
[[SingleLocPlugin instance] getMyLocation];
}