最近自己寫了幾個小項目,項目中用的到一些大家平時開發會常用的一些功能,在此做下記錄,以便后續能快速開發。
《集成百度地圖》
第一步?
登錄百度地圖開發平臺 進入API控制后臺 ? 創建應用 。 ? 應用名稱 ?你的應用名稱 ?安全碼 ?你的App Bundle Identifier ?提交即可;
這下你得到了 最關鍵的Key了
第二步
使用CocoaPods導入地圖SDK
在Podfile文件里添加
pod'BaiduMapKit'#百度地圖SDK
終端CD 到 項目文件夾下 執行 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
看到這些,表示已經加入完成。
第三步
打開項目頭文件 添加百度頭文件
比較多,可以根據自己的業務需要 ,按需添加
第四步
終于可以看到地圖了
@interface AppDelegate:NSObject {
UIWindow*window;
UINavigationController*navigationController;
BMKMapManager*_mapManager;
}
-(BOOL)application:(UIApplication*)application? didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
// 要使用百度地圖,請先啟動
BaiduMapManager_mapManager = [[BMKMapManager alloc]init];
// 如果要關注網絡及授權驗證事件,請設定? ? generalDelegate參數
BOOL ret=[_mapManager start:@"在此處輸入您的授權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,否則影響內存的釋放}
-(void)viewWillDisappear:(BOOL)animated
{[_mapView viewWillDisappear]
;_mapView.delegate=nil;// 不用時,置nil
}
運行~
呦呦呦切克鬧 地圖出來了
第五步
定位獲取自己的地理坐標 ,分地理編碼獲取自己的位置
@interface ZRBMapVC ()<BMKLocationServiceDelegate,BMKMapViewDelegate, BMKGeoCodeSearchDelegate>{
BMKMapView *_mapView;
BMKLocationService *_locService;
BMKGeoCodeSearch *_geoSearch;
}
/** 用戶當前位置*/
@property(nonatomic , strong) BMKUserLocation *userLocation;
/** 當前城市*/
@property (nonatomic, copy) NSString *city;
@end
// 此處記得不用的時候需要置nil,否則影響內存的釋放
-(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,否則影響內存的釋放
_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;//設定地圖是否現顯示3D樓塊效果
_mapView.overlookEnabled = YES; //設定地圖View能否支持俯仰角
_mapView.showMapScaleBar = YES; // 設定是否顯式比例尺
//? ? _mapView.overlooking = -45;? ? // 地圖俯視角度,在手機上當前可使用的范圍為-45~0度
_mapView.zoomLevel = 18;//設置放大級別
_mapView.userTrackingMode = BMKUserTrackingModeNone;//設置定位的狀態
_mapView.showsUserLocation = YES;//顯示定位圖層
[self.view addSubview:_mapView];
_locService = [[BMKLocationService alloc] init];
_locService.distanceFilter = 200;//設定定位的最小更新距離,這里設置 200m 定位一次,頻繁定位會增加耗電量
_locService.desiredAccuracy = kCLLocationAccuracyHundredMeters;//設定定位精度
//開啟定位服務
[_locService startUserLocationService];
_geoSearch = [[BMKGeoCodeSearch alloc] init];
}
/**
*用戶位置更新后,會調用此函數
*@param userLocation 新的用戶位置
*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[_mapView updateLocationData:userLocation];// 動態更新我的位置數據
self.userLocation = userLocation;
[_locService stopUserLocationService];
[_mapView setCenterCoordinate:userLocation.location.coordinate];// 當前地圖的中心點
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
//? ? /// geo檢索信息類,獲取當前城市數據
BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
[_geoSearch reverseGeoCode:reverseGeoCodeOption];
}
#pragma mark - 根據坐標返回反地理編碼搜索結果
-(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 中實現了更多深層功能,如需 請留言~