iOS 百度地图反编码地理位置信息

来源:互联网 发布:域名被墙 301跳转 编辑:程序博客网 时间:2024/04/30 10:35

1:创建

_locService = [[BMKLocationService alloc]init];

    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    _geocodesearch.delegate = self;

    _locService.delegate = self;

2:开始定位

_locService.desiredAccuracy = kCLLocationAccuracyBest;//设置定位精度
 [_locService startUserLocationService];

3:实现delegate

#pragma mark -定位成功
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    NSString *lat = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude];
    NSString *lon = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];
    UZGPersonalSetting *s=[UZGPersonalSetting getInstance];
    s.currLocationl=[NSString stringWithFormat:@"%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude];
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
    if (lat!=nil && lon!=nil) {
        pt = (CLLocationCoordinate2D){[lat floatValue], [lon floatValue]};
    }
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag) NSLog(@"反geo检索发送成功");
}

#pragma mark -定位失败
- (void)didFailToLocateUserWithError:(NSError *)error{
    if (error.code == 1) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"定位服务未开启" message:@"请进入系统设置内开启定位服务(设置-->隐私-->定位服务-->XXX)" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:@"设置", nil];
        [[alert rac_buttonClickedSignal] subscribeNext:^(NSNumber *indexNumber) {
            if ([indexNumber intValue] == 1) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
            }
        }];
        [alert show];
    }else{
        [MBProgressHUD showError:@"定位失败,请稍后再试"];
    }
}

4:实现反编码的delegate

-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    UZGPersonalSetting *s = [UZGPersonalSetting getInstance];
    s.locateAddress = result.address;
    _locateShop = [[ViewControllerHttpLocateShop alloc]init];
    _locateShop.delegate=self;
    [_locateShop initHttpLocateShop];
}

1 1