iOS 实现百度LBS定位
来源:互联网 发布:大司马淘宝店 编辑:程序博客网 时间:2024/05/29 09:09
1. 首先在info.plist 文件中添加两个属性值 都为 YESNSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription2.实现 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();}
0 0
- iOS 实现百度LBS定位
- iOS 百度定位实现
- 百度定位下载 LBS SDK
- LBS定位实现
- ios百度地图定位实现
- Android基于百度定位SDK v4.1(LBS)实现定位功能
- Android使用百度LBS定位服务
- iOS百度地图 定位的实现
- iOS百度地图 定位的实现
- iOS百度地图 定位的实现
- iOS反地理编码,lbs定位
- LBS--定位
- C 简单实现LBS基站定位
- android如何实现LBS定位实例代码
- 百度地图lbs在tabhost中不能定位问题
- 百度LBS
- iOS百度地图定位
- ios LBS定位开发初探(V2.6)
- EF中Expression<Func<T,bool>>动态查询怎么写啊
- 【FFmpeg】Windows下FFmpeg调试
- 【3C认证】安防产品3C认证
- UNITY3D学习笔记5
- 最大连续子列和
- iOS 实现百度LBS定位
- [Android新手区] android里面EditTex多行输入及输入置顶问题
- 工作情况
- mysql的jdbc大量数据操作记录
- 武汉学员,我们和传智播客boss在一起
- Jump Game II
- gzip: stdin: not in gzip format tar: Child returned status 1
- UNIX高级环境编程——进程思考
- 想成为PHP程序员正确的步骤