iOS 实现百度LBS定位

1. 首先在info.plist 文件中添加两个属性值 都为 YES
NSLocationAlwaysUsageDescription   NSLocationWhenInUseUsageDescription

2.实现 BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate  这两个代理方法

@interfaceMKLocationManager()

- (void)getCurrentLocated:(CLLocationCoordinate2D)coordinate;
- (void)stopLocating;


@end


@implementation MKLocationManager
- (id)init
{
    self = [superinit];
    if (self)
    {
        [UIApplicationsharedApplication].idleTimerDisabled =YES;
        //设置定位精确度,默认:kCLLocationAccuracyBest
        [BMKLocationServicesetLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
        //指定最小距离更新(米),默认:kCLDistanceFilterNone
        [BMKLocationServicesetLocationDistanceFilter:100.f];
        _locService = [[BMKLocationServicealloc]init];
        _locService.delegate =self;
        [selfstartLocating];
        
        _geocodesearch = [[BMKGeoCodeSearchalloc]init];
        _geocodesearch.delegate =self;
    }
    
    returnself;
}


+ (MKLocationManager *)shareInstance
{
    staticMKLocationManager *_locationManager;
    staticdispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _locationManager = [[MKLocationManageralloc]init];
    });
    
    return _locationManager;
}


- (void)startLocating{
    [_locServicestartUserLocationService];
}

- (void) stopLocating{
    _locService.delegate =nil;
    _geocodesearch.delegate =nil;
    [_locServicestopUserLocationService];
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    
    CLLocationCoordinate2D pt = userLocation.location.coordinate;
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOptionalloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [_geocodesearchreverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
    }
    
//    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    
}


- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error ==0) {        
        CommonModel *comModel = [CommonModelshareCommonModel];
        if (![comModel.cityNameisKindOfClass:[NSStringclass]] && !comModel.cityName) {
            [comModelsetLatitude:[NSStringstringWithFormat:@"%f",result.location.latitude]];
            [comModelsetLongitude:[NSStringstringWithFormat:@"%f",result.location.longitude]];
            
            NSString *cityName = result.address;
            NSRange range = [cityNamerangeOfString:@"市"];
            NSString *curCity = [cityNamesubstringToIndex:range.location];
            if ([curCityisEqualToString:@""] || ![curCityisKindOfClass:[NSStringclass]]) {
                curCity =@"上海";
            }
            
            NSUserDefaults *udefault = [NSUserDefaultsstandardUserDefaults];
            if (![udefaultobjectForKey:@"location"]) {
                [udefaultremoveObjectForKey:@"location"];
                [udefaultsynchronize];
            }
            
            NSMutableDictionary *mdiction = [NSMutableDictionarydictionaryWithCapacity:1];
            
            NSString *plistPath = [[NSBundlemainBundle]pathForResource:@"citylist"ofType:@"plist"];
            NSArray *dataArr = [NSArrayarrayWithContentsOfFile:plistPath];
            for (int i=0; i<dataArr.count; i++) {
                NSString *cityName = [[dataArr objectAtIndex:i]objectForKey:@"city_name"];
                if ([curCity isEqualToString:cityName]) {
                    [comModelsetCityId:[[dataArrobjectAtIndex:i]objectForKey:@"city_id"]];
                    [comModelsetCityName:cityName];
                    [comModelsetCityInitial:[[dataArrobjectAtIndex:i]objectForKey:@"city_initial"]];
                    [comModelsetLatitude:[NSStringstringWithFormat:@"%f",result.location.latitude]];
                    [comModelsetLongitude:[NSStringstringWithFormat:@"%f",result.location.longitude]];
                    
                    
                    [mdictionsetObject:cityNameforKey:@"city_name"];
                    [mdictionsetObject:[[dataArrobjectAtIndex:i]objectForKey:@"city_id"]forKey:@"city_id"];
                    [mdictionsetObject:[[dataArrobjectAtIndex:i]objectForKey:@"city_initial"]forKey:@"city_initial"];
                }
            }
            
            [mdictionsetObject:[NSStringstringWithFormat:@"%f",result.location.longitude]forKey:@"longitude"];
            [mdictionsetObject:[NSStringstringWithFormat:@"%f",result.location.latitude]forKey:@"latitude"];
            [udefaultsetObject:mdictionforKey:@"location"];
            [udefaultsynchronize];
        }
    }
}

-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{

    if (error ==0) {
        CommonModel *comModel = [CommonModelshareCommonModel];
        if (![comModel.cityNameisKindOfClass:[NSStringclass]] && !comModel.cityName) {
            [comModelsetLatitude:[NSStringstringWithFormat:@"%f",result.location.latitude]];
            [comModelsetLongitude:[NSStringstringWithFormat:@"%f",result.location.longitude]];
            
            NSString *cityName = result.address;
            NSRange range = [cityNamerangeOfString:@"市"];
            NSString *curCity = [cityNamesubstringToIndex:range.location];
            if ([curCityisEqualToString:@""] || ![curCityisKindOfClass:[NSStringclass]]) {
                curCity =@"上海";
            }
            
            
            NSUserDefaults *udefault = [NSUserDefaultsstandardUserDefaults];
            if (![udefaultobjectForKey:@"location"]) {
                [udefaultremoveObjectForKey:@"location"];
                [udefaultsynchronize];
            }
            
            NSMutableDictionary *mdiction = [NSMutableDictionarydictionaryWithCapacity:1];
            
            NSString *plistPath = [[NSBundlemainBundle]pathForResource:@"citylist"ofType:@"plist"];
            NSArray *dataArr = [NSArrayarrayWithContentsOfFile:plistPath];
            for (int i=0; i<dataArr.count; i++) {
                NSString *cityName = [[dataArr objectAtIndex:i]objectForKey:@"city_name"];
                if ([curCity isEqualToString:cityName]) {
                    [comModelsetCityId:[[dataArrobjectAtIndex:i]objectForKey:@"city_id"]];
                    [comModelsetCityName:cityName];
                    [comModelsetCityInitial:[[dataArrobjectAtIndex:i]objectForKey:@"city_initial"]];
                    [comModelsetLatitude:[NSStringstringWithFormat:@"%f",result.location.latitude]];
                    [comModelsetLongitude:[NSStringstringWithFormat:@"%f",result.location.longitude]];
                    
                    [mdictionsetObject:cityNameforKey:@"city_name"];
                    [mdictionsetObject:[[dataArrobjectAtIndex:i]objectForKey:@"city_id"]forKey:@"city_id"];
                    [mdictionsetObject:[[dataArrobjectAtIndex:i]objectForKey:@"city_initial"]forKey:@"city_initial"];
                }
            }

            [mdictionsetObject:[NSStringstringWithFormat:@"%f",result.location.longitude]forKey:@"longitude"];
            [mdictionsetObject:[NSStringstringWithFormat:@"%f",result.location.latitude]forKey:@"latitude"];
            [udefaultsetObject:mdictionforKey:@"location"];
            [udefaultsynchronize];
            
        }
    }
}

- (void)getCurrentLocated:(CLLocationCoordinate2D)coordinate{
    CommonModel *model = [CommonModelshareCommonModel];
    coordinate.latitude = [model.latitudedoubleValue];
    coordinate.longitude = [model.longitudedoubleValue];
}



- (void)dealloc
{
    if (_locService !=nil) {
        _locService =nil;
    }
    if (_geocodesearch !=nil) {
        _geocodesearch =nil;
    }
    [selfstopLocating];
    SAFE_ARC_SUPER_DEALLOC();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值