整理了一下項目中的定位功能,對百度地圖的封裝總結記錄一下
首先通過CocoaPods引入百度 pod ‘BaiduMapKit’
然後爲了更方便的在項目中使用它,對其中BMKMapManager這個類進行繼承封裝
在這個繼承類LocManager中,進行一系列的操作:
1.設置全局訪問點
+ (instancetype)shared
{
static LocManager *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[LocManager alloc] init];
});
return instance;
}
2.申請百度地圖key,進行授權,由於繼承自BMKMapManager,所以可以在類中自由的使用其中的方法
//設置key。開啓授權堅定
-(void)setBaiduKey
{
BOOL ret = [self start:baiduKey generalDelegate:self];
if (!ret) {
JKLog(@"manager start failed!");
}
}
3.判斷是否授權成功,回調出去,對成功或失敗做具體的操作
/**
*返回授權驗證錯誤
*@param iError 錯誤號 : 爲0時驗證通過,具體參加BMKPermissionCheckResultCode
*/
- (void)onGetPermissionState:(int)iError
{
if (iError == 0)
{
NSLog(@"授權驗證成功");
if (self.BMKPermissionsStateBlock)
{
self.BMKPermissionsStateBlock(YES);
}
}else{
NSLog(@"授權驗證gg");
if (self.BMKPermissionsStateBlock)
{
self.BMKPermissionsStateBlock(NO);
}
[self stop];
}
}
4.授權成功之後,對外暴露一個方法,用來配置一些參數,以及啓動定位服務,這裏面主要是一些系統的權限,其中要保證這些參數爲全局的,保證系統的權限彈窗不會消失
保證這幾個參數爲全局的
CLLocationManager *locationManager;
CLAuthorizationStatus locationStatus;
BMKLocationService *locService;
BMKGeoCodeSearch *geocodesearch;
//開啓定位服務
locService = [[BMKLocationService alloc] init];
locService.delegate = self;
[locService startUserLocationService];
geocodesearch = [[BMKGeoCodeSearch alloc] init];
geocodesearch.delegate = self;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationStatus = [CLLocationManager authorizationStatus];
[locationManager requestAlwaysAuthorization];//一直獲取定位信息
[locationManager requestWhenInUseAuthorization];//使用的時候獲取定位信息
5.定位成功和失敗的代理,定位成功之後反geo檢索
其中:
正向地理編碼指的是由地址信息轉換爲座標點的過程。
反向地理編碼指的是將地球表面的地址座標轉換爲標準地址的過程。
//定位失敗
-(void)didFailToLocateUserWithError:(NSError *)error
{
[locService stopUserLocationService];
}
//定位成功 當用戶的位置更新後,也會調用這裏,在更新完成的最後,關閉定位
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
pt = (CLLocationCoordinate2D){userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};
BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeocodeSearchOption.reverseGeoPoint = pt;
BOOL flag = [geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
if(flag)
{
//反geo檢索發送成功
}
else
{
//反geo檢索發送失敗
}
[locService stopUserLocationService];
}
6.每當定位權限發生改變時,都會觸發的方法
//改變授權狀態時
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
}
7.其中最重要的兩個方法:地址信息搜索結果,反地理編碼搜索結果
/**
*返回地址信息搜索結果
*@param searcher 搜索對象
*@param result 搜索結BMKGeoCodeSearch果
*@param error 錯誤號,@see BMKSearchErrorCode
*/
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;
可以得到地址編碼的經緯度信息以及地址名稱
/**
*返回反地理編碼搜索結果
*@param searcher 搜索對象
*@param result 搜索結果
*@param error 錯誤號,@see BMKSearchErrorCode
*/
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;
其中反地理編碼搜索結果,可以得到當前定位附近的主要信息
這裏面的poiList包含的是這附近的10條位置信息
//返回反地理編碼搜索結果 輸入指定的位置進行搜索時,也通過這裏返回得到的位置信息
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
if (error == BMK_SEARCH_NO_ERROR) {
//取最近的第一條位置信息進行處理
BMKPoiInfo* poi = result.poiList.firstObject;
//當前位置名字
poi.name
//當前位置的經緯度信息
[NSString stringWithFormat:@"%f",poi.pt.latitude];
[NSString stringWithFormat:@"%f",poi.pt.longitude];
}
}
8.根據輸入的指定位置,搜索時返回的位置信息
/**
*返回POI搜索結果
*@param searcher 搜索對象
*@param poiResult 搜索結果列表
*@param errorCode 錯誤號,@see BMKSearchErrorCode
*/
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode;
其中返回的結果包含
///本次POI搜索的總結果數
@property (nonatomic) int totalPoiNum;
///當前頁的POI結果數
@property (nonatomic) int currPoiNum;
///本次POI搜索的總頁數
@property (nonatomic) int pageNum;
///當前頁的索引
@property (nonatomic) int pageIndex;
///POI列表,成員是BMKPoiInfo
@property (nonatomic, strong) NSArray* poiInfoList;
///城市列表,成員是BMKCityListInfo
@property (nonatomic, strong) NSArray* cityList;
其中poiInfoList這個數組裏面,每一條都包含了如下信息
NSString* _name; ///<POI名稱
NSString* _uid;
NSString* _address; ///<POI地址
NSString* _city; ///<POI所在城市
NSString* _phone; ///<POI電話號碼
NSString* _postcode; ///<POI郵編
int _epoitype; ///<POI類型,0:普通點 1:公交站 2:公交線路 3:地鐵站 4:地鐵線路
CLLocationCoordinate2D _pt; ///<POI座標
在這個自定義繼承自BMKMapManager的類中,可以處理一些與自己服務器後臺接口相關的操作,比如通過
定位的信息進行狀態判斷,APP有無定位給用戶的展示,再比如有關物流的應用,通過當前的經緯度判斷是否在配送範圍等等