iOS系統內部的地圖在國內使用的是高德地圖,因爲官方文檔的限制百度地圖其實也是對系統地圖API的二次封裝而已。因爲公司的項目是一款與物流相關的APP,主界面是以地圖爲藍本,因此就必須對百度地圖十分熟悉(項目的老版本使用的百度地圖)。app
第一步、 在appDelegate的didFinishLaunchingWithOptions:方法中進行百度地圖的初始化操做:函數
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { mapManager = [[BMKMapManager alloc]init]; BOOL ret = [mapManager start:BAIDU_MAP_KEY generalDelegate:self];//BAIDU_MAP_KEY爲百度註冊獲取的Key if(!ret) { NSLog(@"manager start failed!"); } }
第二步、在展現地圖view的controller中實例化BMKMapView對象並添加相關UI控件編碼
BDMapView=[[BMKMapView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width ,self.frame.size.height)]; BDMapView.zoomEnabled=true; BDMapView.showMapScaleBar =true; BDMapView.mapScaleBarPosition = CGPointMake(10,BDMapView.frame.size.height - 45); BDMapView.zoomLevel=15; //自定義精度圈 BMKLocationViewDisplayParam * param = [[BMKLocationViewDisplayParam alloc]init]; param.isAccuracyCircleShow = NO;//去掉大圓圈 param.locationViewImgName=@"icon_center_point"; [BDMapView updateLocationViewWithParam:param]; //定位按鈕UI UIButton *btnLocation = [UIButton buttonWithType:UIButtonTypeCustom]; btnLocation.frame=CGRectMake(self.frame.size.width-50,BDMapView.mapScaleBarPosition.y-40,35,35); btnLocation.backgroundColor = WhiteColor; [btnLocation.layer setMasksToBounds:YES]; [btnLocation.layer setCornerRadius:AppCornerRadius]; [btnLocation setBackgroundImage:[ColorUtils createImageWithColor:WhiteColor rect:btnLocation.frame]forState:UIControlStateNormal]; [btnLocation addTarget:self action:@selector(locationOnDown:)forControlEvents:UIControlEventTouchDown]; //添加控件在View上面 [self addSubview:BDMapView]; [self addSubview:btnLocation];
#pragma mark - 定位按鈕按下 -(void)locationOnDown:(UIButton*)button { [button setBackgroundColor:CheckColor]; [self onStart]; }
至此,調用定位方法。在項目中爲方便使用結合API對其進行了二次封裝。若是咱們只是定位的話比較簡單隻須要是使用初始化BMKLocationService類並調用其對象方法便可以下:spa
-(id)init { locService = [[BMKLocationService alloc]init]; return self; } #pragma mark 開始定位 -(void)onStartLocation { [locService startUserLocationService]; } #pragma mark 結束定位 -(void)onStopLocation { [locService stopUserLocationService]; }
若是咱們還須要獲取詳細的省市縣三級地址或者經緯度的話稍微麻煩一點。須要遵照兩個代理:代理
代理一:BMKLocationServiceDelegatecode
代理二:BMKGeoCodeSearchDelegateorm
遵照代理一 是由於須要用到其內部方法對象
/** *用戶位置更新後,會調用此函數 *@param userLocation 新的用戶位置 */ - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation;
遵照代理二 是由於須要用到其內部方法ci
/** *返回反地理編碼搜索結果 *@param searcher 搜索對象 *@param result 搜索結果 *@param error 錯誤號,@see BMKSearchErrorCode */ - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;
是否是到此就算結束了?錯!
文檔
還有一些小問題須要解決:
問題1:
//在定位的方法裏將定位獲取的位置傳給百度geo的位置這樣才能定位成功並顯示!!! #pragma mark 定位成功返回(調用位置更新的方法) - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation { if(userLocation.location!=nil) { BMKUserLocation *bmkUserLocation = userLocation; CLLocationCoordinate2D position = userLocation.location.coordinate; BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init]; reverseGeocodeSearchOption.reverseGeoPoint = position; geocodesearch.delegate=self; [geocodesearch reverseGeoCode:reverseGeocodeSearchOption]; } else { [delegate onLocationError:@"定位異常..." addressFlag:false]; } }
爲題2:
#pragma mark 反向地理編碼,經緯度轉地址 -(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error { //檢索結果正常返回 if (error == 0) { BMKAddressComponent *address = result.addressDetail; [delegate onLocationAddress:address.province city:address.city town:address.district street:[address.streetName stringByAppendingString:address.streetNumber]]; //顯示經緯度 [delegate onLocationAngle:bmkUserLocation]; } else { [delegate onLocationError:@"經緯度轉地址-地址編碼未實現..." addressFlag:true]; } }
備註:代碼中所涉及到的delegate爲二次封裝的代理。針對百度地圖二次封裝的源碼待我百度網盤弄好以後會以超連接的形式共享出來。