oc 获取经纬度具体位置

1,再 build phases 中的 link binary with libraries  中 导入 CoreLocation.framework 库

2,包含:#import <CoreLocation/CoreLocation.h>

3,特别需要注意的: iPhone 机 需要链接网络, 否则获取的 placemarks  is null

4,在info.plist  中添加 

<key>NSLocationWhenInUseUsageDescription</key>
  <string>此App需要您的同意才能访问您的位置</string>

@interface LocationTest : NSObject<CLLocationManagerDelegate>{

    CLLocationDegrees lati;
    CLLocationDegrees longti;
}
@property(nonatomic,strong)CLLocationManager *locationManager;

-(void)InitLocation;
//用户位置获取

-(void)InitLocation{
    //初始化对象
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 1.0;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
//        [self.locationManager requestAlwaysAuthorization]; // 永久授权
        [self.locationManager requestWhenInUseAuthorization]; //使用中授权
    }
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"%@", error);
}
// 代理方法 地理位置反编码
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  
    CLLocation *newlocation = locations[0];
    CLLocationCoordinate2D oCoordinate = newlocation.coordinate;
    NSLog(@"经度:%f,维度:%f",oCoordinate.longitude,oCoordinate.latitude);
    // 给经纬度全局属性赋值
    lati = oCoordinate.latitude;
    longti = oCoordinate.longitude;
    [self.locationManager  stopUpdatingLocation];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:newlocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
        NSLog(@"----------reverseGeocodeLocation---------");
        NSString* placeLity = @"";
        NSString* str = @"";
        for (CLPlacemark *place in placemarks) {
            //            CLPlacemark* place = placemarks[0];
            NSDictionary *location =[place addressDictionary];
            NSLog(@"国家:%@",[location objectForKey:@"Country"]);
            NSLog(@"城市:%@",[location objectForKey:@"State"]);
            NSLog(@"区:%@",[location objectForKey:@"SubLocality"]);
            NSLog(@"位置:%@", place.name);
            NSLog(@"国家:%@", place.country);
            NSLog(@"城市:%@", place.locality);
            placeLity = place.locality;
            NSLog(@"区:%@", place.subLocality);
            NSLog(@"街道:%@", place.thoroughfare);
            NSLog(@"子街道:%@", place.subThoroughfare);
            //字符串拼接
            str = [NSString stringWithFormat:@"%@%@%@", place.locality, place.subLocality,place.thoroughfare];
            
        }
        NSLog(@"----- 位置  %@",str);
        [LocationTest CallJsLocation:str andLati:lati andLongti:longti];
    }];

    NSLog(@"=====:%f,%f",lati,longti);
    
}
+(void)CallJsLocation:(NSString*)Adree  andLati:(float)lati andLongti:(float)longti{
    
    NSLog(@"%@  %f   %f",Adree,lati,longti);
    NSString *func = [NSString stringWithFormat:@"require('jsbScript').responseLocation('%@',%f,%f)",Adree,lati,longti];
    // 转为C风格字符串
    const char *stringFunc = [func UTF8String];
    // OC调用JS
    ScriptingCore::getInstance()->evalString(stringFunc);
    
}
//开启位置
+(void)StartLocation{
    NSLog(@"---StartLocation----");
    //初始化对象
    LocationTest* locat = [[LocationTest alloc]init];
    [locat InitLocation];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值