#import <Foundation/Foundation.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#define MYLOCATION @"mylocation"
#define PROVINCES @"provinces"
#define CITYNAME @"cityName"
#define LOCATION @"location"
typedef void(^updateStateBlock)(NSString *);
typedef void(^updateCityBlock)();
@interface baiduMapTool:NSObject<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,UIAlertViewDelegate>
{
CLLocation *cllocation;
}
@property (strong,nonatomic) BMKLocationService *locService;
//反地理编码
@property (nonatomic, strong)BMKGeoCodeSearch * geocodesearch ;
//用户纬度
@property (nonatomic,assign) double userLatitude;
//用户经度
@property (nonatomic,assign) double userLongitude;
/**
* 是否改变过城市
*/
@property(nonatomic, assign) BOOL changeCity;
//回调更新状态
@property (strong, nonatomic)updateStateBlock updateStateBlock;
//回调城市更新状态
@property (strong, nonatomic)updateCityBlock updateCityBlock;
//初始化单例
+ (baiduMapTool *)sharedInstance;
//初始化百度地图用户位置管理类
- (void)initBMKUserLocation;
//开始定位
-(void)startLocation;
//停止定位
-(void)stopLocation;
//根据城市获取经纬度
- (void)getCllcotion:(NSString *)cityName;
@end
#import "baiduMapTool.h"
@implementation baiduMapTool
#pragma mark - 初始化信息
static baiduMapTool *_instance = nil;
+ (baiduMapTool *)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_instance == nil) {
_instance = [[self alloc] init];
}
});
return _instance;
}
-(id)init
{
self = [super init];
if (self)
{
[self initBMKUserLocation];
}
return self;
}
- (void)initBMKUserLocation
{
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
_geocodesearch = [[BMKGeoCodeSearch alloc]init];
_geocodesearch.delegate = self;
}
#pragma 打开定位服务
/**
* 打开定位服务
*/
-(void)startLocation
{
[_locService startUserLocationService];
}
#pragma 关闭定位服务
/**
* 关闭定位服务
*/
-(void)stopLocation
{
[_locService stopUserLocationService];
}
#pragma BMKLocationServiceDelegate
#pragma mark - 关闭定位后调用
- (void)didStopLocatingUser
{
}
#pragma mark - 根据经纬度获取城市
- (void)reverseCity{
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];//初始化反编码请求
reverseGeocodeSearchOption.reverseGeoPoint =cllocation.coordinate;//设置反编码
NSLog(@"%f%f",cllocation.coordinate.latitude,cllocation.coordinate.longitude);
BOOL flag = [self.geocodesearch reverseGeoCode:reverseGeocodeSearchOption];//发送反编码请求.并返回是否成功
if(flag)
{
NSLog(@"反geo检索发送成功");
}else{
NSLog(@"反geo检索发送失败");
}
[self stopLocation];
}
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR)
{
NSString * _cityName = result.addressDetail.city;
NSString * choseCity = [_cityName substringToIndex:_cityName.length];
// 存储定位城市信息
NSString * province = result.addressDetail.province;
NSDictionary * myLocation = @{PROVINCES:province
,CITYNAME:choseCity};
[[NSUserDefaults standardUserDefaults]setObject:myLocation forKey:MYLOCATION];
NSDictionary * dic = [[NSUserDefaults standardUserDefaults]objectForKey:LOCATION];
if (dic)
{
if (![choseCity isEqualToString:[dic objectForKey:CITYNAME]])
{
if (!_changeCity)
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"系统定位您现在在%@,是否切换到%@",choseCity,choseCity] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"切换",nil];
[alert show];
_changeCity = YES;
}
}
}
else
{
_changeCity = YES;
[[NSUserDefaults standardUserDefaults]setObject:myLocation forKey:LOCATION];
}
self.updateCityBlock();
// NSDictionary * myLocation = @{PROVINCES:province
// ,CITYNAME:choseCity};
// [[NSUserDefaults standardUserDefaults]setObject:myLocation forKey:MYLOCATION];
}
else
{
NSLog(@"未找到结果 %u",error);
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
NSDictionary * dic = [[NSUserDefaults standardUserDefaults]objectForKey:MYLOCATION];
[[NSUserDefaults standardUserDefaults]setObject:dic forKey:LOCATION];
self.updateCityBlock();
}
}
#pragma mark ----根据城市名获取经纬度
- (void)getCllcotion:(NSString *)cityName{
BMKGeoCodeSearchOption *geocodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
geocodeSearchOption.city= cityName;
geocodeSearchOption.address = cityName;
NSLog(@"%@",cityName);
BOOL flag = [_geocodesearch geoCode:geocodeSearchOption];
if(flag)
{
NSLog(@"geo检索发送成功");
}
else
{
NSLog(@"geo检索发送失败");
}
}
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
NSLog(@"onGetGeoCodeResult error %d", error);
if (error == 0) {
BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
item.coordinate = result.location;
item.title = result.address;
// @"经度:%f,纬度:%f",item.coordinate.latitude,item.coordinate.longitude];
NSString *userLong = [NSString stringWithFormat:@"%f", item.coordinate.longitude];
NSString *userLat = [NSString stringWithFormat:@"%f", item.coordinate.latitude];
[[NSUserDefaults standardUserDefaults] setObject:userLong forKey:@"userLongitude"];
[[NSUserDefaults standardUserDefaults] setObject:userLat forKey:@"userLatitude"];
}
else
{
NSLog(@"抱歉,未找到结果");
}
}
/**
* 根据经纬度获取位置
*
* @return 城市名
*/
#pragma mark ----BMK_LocationDelegate
- (void)didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"%@",error.domain);
//获取成功/失败状态
if (_updateStateBlock)
{
_updateStateBlock(@"error");
}
[[NSUserDefaults standardUserDefaults] removeObjectForKey:MYLOCATION];
[self stopLocation];
}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
cllocation = userLocation.location;
_userLatitude = cllocation.coordinate.latitude;
_userLongitude = cllocation.coordinate.longitude;
NSString *userLong = [NSString stringWithFormat:@"%f", _userLongitude];
NSString *userLat = [NSString stringWithFormat:@"%f", _userLatitude];
[[NSUserDefaults standardUserDefaults] setObject:userLong forKey:@"userLongitude"];
[[NSUserDefaults standardUserDefaults] setObject:userLat forKey:@"userLatitude"];
NSLog(@"%@,%@",userLat,userLong);
// 获取成功/失败状态
//获取成功/失败状态
if (_updateStateBlock) {
_updateStateBlock(@"success");
[self reverseCity];
}
//关闭坐标更新
}
@end