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();
}
iOS 实现百度LBS定位
最新推荐文章于 2021-03-02 14:12:41 发布