IOS 定位 CLLocationManager

前端 unity

U3D C#调用 Object-c方法,开始定位,获取到位置抛送unity事件给C#端 停止

IOS组件需要 CLLocationManager

XCode info里加个

1.Privacy - Location When In Use Usage Description -> 是否允许此App在使用期间访问你的位置?
2.Privacy - Location Always Usage Description -> 是否允许此App永久访问你的位置?(这个不建议使用)

需要两个文件

IOS_LocationController.h

//
//  IOS_LocationController.h
//  Unity-iPhone
//
//  Created by hoolai on 2018/11/27.
//

//#ifndef IOS_LocationController_h
//#define IOS_LocationController_h
#import <CoreLocation/CoreLocation.h>
@interface IOS_LocationController : UIViewController<CLLocationManagerDelegate>//location代理继承
@property (nonatomic,strong) CLLocationManager *locationManager;
@end

//#endif /* IOS_LocationController_h */

IOS_LocationController.m

//
//  IOS_LocationController.m
//  Unity-iPhone
//
//  Created by hoolai on 2018/11/27.
//

#import "IOS_LocationController.h"
#import <CoreLocation/CoreLocation.h>

@implementation IOS_LocationController
- (void)createLocationManager{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    //    [self.locationManager requestAlwaysAuthorization];
    [self.locationManager requestWhenInUseAuthorization];//获取访问权限。可以在info.plist里面填写给用户的请求信息
    // 设置定位精度
    // kCLLocationAccuracyNearestTenMeters:精度10米
    // kCLLocationAccuracyHundredMeters:精度100 米
    // kCLLocationAccuracyKilometer:精度1000 米
    // kCLLocationAccuracyThreeKilometers:精度3000米
    // kCLLocationAccuracyBest:设备使用电池供电时候最高的精度
    // kCLLocationAccuracyBestForNavigation:导航情况下最高精度,一般要有外接电源时才能使用
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    // distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序
     self.locationManager.distanceFilter = kCLDistanceFilterNone; // 如果设为kCLDistanceFilterNone,则每秒更新一次;
}
-(void)startLocation
{
    [self createLocationManager];
    [self.locationManager startUpdatingLocation];//开始定位
}
#pragma mark - CLLocationManagerDelegate
// 地理位置发生改变时触发
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // 获取经纬度
    NSLog(@"纬度:%f",newLocation.coordinate.latitude);
    NSLog(@"经度:%f",newLocation.coordinate.longitude);
    // 停止位置更新
    [manager stopUpdatingLocation];
    //根据经纬度获取省份城市
    CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
    [clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
        for (CLPlacemark *placeMark in placemarks)
        {
            
            
            NSDictionary *addressDic=placeMark.addressDictionary;//地址的所有信息
            
            NSString *state=[addressDic objectForKey:@"State"];//省。直辖市  江西省
            NSString *city=[addressDic objectForKey:@"City"];//市  丰城市
            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];//区
            //            NSString *street=[addressDic objectForKey:@"Street"];//街道
            NSLog(@"%@=====%@-----%@=====%@",addressDic,state,city,subLocality);
            
            NSString *st=@"";
            st = [NSString stringWithFormat:@"%@,%@,%@", state, city ,subLocality];
            const char *pConstChar;
            pConstChar = [st UTF8String];
            //UnitySendMessage 是用来给unity发消息的  有三个参数 1.挂载对应回调脚本的物体名 2.回调函数的名称 3.对应回调上的参数
            UnitySendMessage("Root", "locationOK", pConstChar);
            break;
        }
        
    }];
    
    
}

// 定位失误时触发
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"error:%@----%ld",error,(long)[error code]);
    UnitySendMessage("Root", "locationOK", "null");
    if ([error code] == 1) {
        //没有位置访问权限
    }
}
@end
#if defined(__cplusplus)
extern "C" {
#endif
    IOS_LocationController *app ;
    //导出接口供unity使用
    void IOS_Location(){
        app = [[IOS_LocationController alloc]init];
        [app startLocation];
    }
#if defined(__cplusplus)
}
#endif

猜你喜欢

转载自blog.csdn.net/qq_18709863/article/details/84573686