您的位置:首页 > 移动开发 > IOS开发

iOS 获取用户位置信息的 预处理

2016-04-13 16:36 459 查看
// 在info.plist 添加 可以通过配置NSLocationAlwaysUsageDescription或者NSLocationWhenInUseUsageDescription来告诉用户使用定位服务的目的(一直、、当用户使用时)

// 设置地图

- (void)setMap {

    // 可先进行网络判断

    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)) {

        

        //定位功能可用,开始定位

         _cllocationManager = [[CLLocationManager alloc]init];

         // 提前调用此方法(程序打开的第一页面) 让用户授权可否访问位置信息

         [_cllocationManager requestWhenInUseAuthorization];//(当用户使用时)

         _cllocationManager.delegate = self;

         _cllocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

        [_cllocationManager startUpdatingLocation];

        }

    

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {

            //NSLog(@"(((((((((用户不允许定位)))))))))");

            // 定位不可用 —— 传虚拟经纬度

            lati = 0.000000;

            longti = 0.000000;

            // 用虚拟位置获取数据

        }

}

// 地理位置反编码 方法 根据经纬度 获取街道名

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {

    

   // NSLog(@"5");

    CLLocation *newlocation = locations[0];

    CLLocationCoordinate2D oCoordinate = newlocation.coordinate;

    //NSLog(@"经度:%f,维度:%f",oCoordinate.longitude,oCoordinate.latitude);

    // 给经纬度全局属性赋值

    lati = oCoordinate.latitude;

    longti = oCoordinate.longitude;

 

    

    [NSTimer scheduledTimerWithTimeInterval:8.0 target:self selector:@selector(action:) userInfo:nil repeats:nil];

    [_cllocationManager stopUpdatingLocation];

    

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:newlocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        for (CLPlacemark *place in placemarks) {

            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);

            NSLog(@"区:%@", place.subLocality);

            NSLog(@"街道:%@", place.thoroughfare);

            NSLog(@"子街道:%@", place.subThoroughfare);

        }

    }];

//    NSLog(@"@@@@@@@@@@=====%f,%f",lati,longti);

 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: