最近自己寫了幾個小項目,項目中用的到一些大家平時開發(fā)會常用的一些功能,在此做下記錄,以便后續(xù)能快速開發(fā)。
《集成百度地圖》
第一步?
登錄百度地圖開發(fā)平臺 進(jìn)入API控制后臺 ? 創(chuàng)建應(yīng)用 。 ? 應(yīng)用名稱 ?你的應(yīng)用名稱 ?安全碼 ?你的App Bundle Identifier ?提交即可;
這下你得到了 最關(guān)鍵的Key了
第二步
使用CocoaPods導(dǎo)入地圖SDK
在Podfile文件里添加
pod'BaiduMapKit'#百度地圖SDK
終端CD 到 項目文件夾下 執(zhí)行 pod install 耐心等待一下 ?
Analyzing dependencies?
Downloading dependencies?
Installing BaiduMapKit(2.9.1)
Generating Pods project?
Integrating client project
[!]Pleasecloseany current Xcode sessions anduse`***.xcworkspace`forthisproject from now on.Sendingstats
看到這些,表示已經(jīng)加入完成。
第三步
打開項目頭文件 添加百度頭文件
比較多,可以根據(jù)自己的業(yè)務(wù)需要 ,按需添加
第四步
終于可以看到地圖了
@interface AppDelegate:NSObject {
UIWindow*window;
UINavigationController*navigationController;
BMKMapManager*_mapManager;
}
-(BOOL)application:(UIApplication*)application? didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
// 要使用百度地圖,請先啟動
BaiduMapManager_mapManager = [[BMKMapManager alloc]init];
// 如果要關(guān)注網(wǎng)絡(luò)及授權(quán)驗證事件,請設(shè)定? ? generalDelegate參數(shù)
BOOL ret=[_mapManager start:@"在此處輸入您的授權(quán)Key"generalDelegate:nil];
if(!ret){NSLog(@"manager start failed!");}
// Add the navigation controller's view to the window and display.[self.windowaddSubview:navigationController.view];
[self.windowmakeKeyAndVisible];returnYES;}
-(void)viewDidLoad{
[superviewDidLoad];
BMKMapView*mapView=[[BMKMapView alloc]initWithFrame:self.view.bounds];
self.view=mapView;
}
-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate=self;
// 此處記得不用的時候需要置nil,否則影響內(nèi)存的釋放}
-(void)viewWillDisappear:(BOOL)animated
{[_mapView viewWillDisappear]
;_mapView.delegate=nil;// 不用時,置nil
}
運行~
呦呦呦切克鬧 地圖出來了
第五步
定位獲取自己的地理坐標(biāo) ,分地理編碼獲取自己的位置
@interface ZRBMapVC ()<BMKLocationServiceDelegate,BMKMapViewDelegate, BMKGeoCodeSearchDelegate>{
BMKMapView *_mapView;
BMKLocationService *_locService;
BMKGeoCodeSearch *_geoSearch;
}
/** 用戶當(dāng)前位置*/
@property(nonatomic , strong) BMKUserLocation *userLocation;
/** 當(dāng)前城市*/
@property (nonatomic, copy) NSString *city;
@end
// 此處記得不用的時候需要置nil,否則影響內(nèi)存的釋放
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時,置nil
_locService.delegate = nil;
_geoSearch.delegate = nil;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內(nèi)存的釋放
_locService.delegate = self;
_geoSearch.delegate = self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"獲取地位";
[self setupMapViewWithParam];
}
-(void)setupMapViewWithParam
{
self.userLocation = [[BMKUserLocation alloc] init];
//初始化BMKMapView
_mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
_mapView.buildingsEnabled = YES;//設(shè)定地圖是否現(xiàn)顯示3D樓塊效果
_mapView.overlookEnabled = YES; //設(shè)定地圖View能否支持俯仰角
_mapView.showMapScaleBar = YES; // 設(shè)定是否顯式比例尺
//? ? _mapView.overlooking = -45;? ? // 地圖俯視角度,在手機上當(dāng)前可使用的范圍為-45~0度
_mapView.zoomLevel = 18;//設(shè)置放大級別
_mapView.userTrackingMode = BMKUserTrackingModeNone;//設(shè)置定位的狀態(tài)
_mapView.showsUserLocation = YES;//顯示定位圖層
[self.view addSubview:_mapView];
_locService = [[BMKLocationService alloc] init];
_locService.distanceFilter = 200;//設(shè)定定位的最小更新距離,這里設(shè)置 200m 定位一次,頻繁定位會增加耗電量
_locService.desiredAccuracy = kCLLocationAccuracyHundredMeters;//設(shè)定定位精度
//開啟定位服務(wù)
[_locService startUserLocationService];
_geoSearch = [[BMKGeoCodeSearch alloc] init];
}
/**
*用戶位置更新后,會調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];// 動態(tài)更新我的位置數(shù)據(jù)
self.userLocation = userLocation;
[_locService stopUserLocationService];
[_mapView setCenterCoordinate:userLocation.location.coordinate];// 當(dāng)前地圖的中心點
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//? ? /// geo檢索信息類,獲取當(dāng)前城市數(shù)據(jù)
BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
[_geoSearch reverseGeoCode:reverseGeoCodeOption];
}
#pragma mark - 根據(jù)坐標(biāo)返回反地理編碼搜索結(jié)果
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
BMKAddressComponent *addressComponent = result.addressDetail;
self.city = addressComponent.city;
NSString *title = [NSString stringWithFormat:@"%@%@%@%@", addressComponent.city, addressComponent.district, addressComponent.streetName, addressComponent.streetNumber];
NSLog(@"%s -- %@", __func__, title);
}
至此 獲取就獲取自己的定位了。demo 中實現(xiàn)了更多深層功能,如需 請留言~