iOS 定位

来源:互联网 发布:开源软件移植 编辑:程序博客网 时间:2024/05/16 17:23
 //创建定位管理者    _locationManager = [[CLLocationManager alloc]init];        //是否开启定位    if ([CLLocationManager locationServicesEnabled]) {        //是否授权        if ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse) {            //申请授权 与plist文件一致  addrow (NSLocationWhenInUseUsageDescription)            [_locationManager requestWhenInUseAuthorization];        }else{            //要求的精度            /*             kCLLocationAccuracyBestForNavigation  //导航用            kCLLocationAccuracyBest;  //最好的             */            _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;                        _locationManager.delegate = self;            //开启定位            [_locationManager startUpdatingLocation];                        _locationHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];            _locationHud.labelText = @"正在定位...";        }    }#pragma mark  - CLLocationManagerDelegate 代理- (void)locationManager:(CLLocationManager *)manager     didUpdateLocations:(NSArray *)locations{    //1.停止定位    [manager stopUpdatingLocation];        //新浪反编码    /*     CLLocation *location = [locations lastObject];     CLLocationCoordinate2D coordinate = location.coordinate;     double latitude = coordinate.latitude;     double longitude = coordinate.longitude;         NSString * paramasTring = [NSString stringWithFormat:@"%f,%f",longitude,latitude];    NSDictionary *dic = @{@"coordinate":paramasTring};    [DataNetSeveice requestWithURLStr:@"location/geo/geo_to_address.json" httpMethod:@"GET" params:[dic mutableCopy] fileData:nil success:^(id result) {                NSDictionary *geosDict = [result[@"geos"] lastObject];        NSString *address = geosDict[@"address"];        NSLog(@"定位成功:%@",address);    } failed:^(NSError *erro) {    }];    */        //iOS8 反编码    //创建地理编码对象    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];    //进行反编码    [geoCoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray *placemarks, NSError *error) {        //  NSLog(@"%@",placemarks);        //地理信息        CLPlacemark *mark = [placemarks lastObject];                //        NSLog(@"%@",mark);        //获取存储地理位置的字典        NSDictionary *dic = mark.addressDictionary;        NSString *adress = dic[@"SubLocality"];//        NSLog(@"%@",adress);        self.locationLabel.text = adress;        [_locationLabel sizeToFit];        _locationHud.hidden = YES; //隐藏定位风火轮    }];        }


0 0