標籤:
使用cocospod匯入百度地圖 詳情見文檔
使用 百度地圖 需要擷取密鑰
2 配置 項目
info中
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
保證網路可用
<key>LSApplicationQueriesSchemes</key> <array> <string>baidumap</string> </array>
保證百度地圖可調用
匯入功能檔案頭部
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關所有的標頭檔 #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的標頭檔 #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的標頭檔 #import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入雲檢索功能所有的標頭檔 #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的標頭檔 #import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計算工具所有的標頭檔 #import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達功能所有的標頭檔 #import < BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個標頭檔
定位
初始化百度地圖 以及 定位
mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 80, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 50)];
[self.view addSubview: mapView];
locService = [[BMKLocationService alloc]init];
設定代理
locService.delegate = self;
//啟動LocationService
[locService startUserLocationService];
//以下_mapView為BMKMapView對象
代理
#pragma mark - locationInfomation(使用者位置資訊)
/**
*使用者方向更新後,會調用此函數
*@param userLocation 新的使用者位置
*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if(userLocation){
NSLog(@"heading is %@",userLocation.heading);
}
});
}
//處理位置座標更新 定位當前位置
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
if(userLocation){
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//當前經緯度
NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
_str = [NSMutableString stringWithFormat:@"%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude];
self.latitude = userLocation.location.coordinate.latitude;
self.longitude = userLocation.location.coordinate.longitude;
//更新我的位置資料
[mapView updateLocationData:userLocation];
///geo搜尋服務 (將經緯度轉化為地址,城市等資訊,被稱為反向地理編碼)
self.searcher = [[BMKGeoCodeSearch alloc]init];
self.searcher.delegate = self;
CLLocationCoordinate2D point = (CLLocationCoordinate2D){self.latitude,self.longitude};
BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
reverseGeoCodeSearchOption.reverseGeoPoint = point;
BOOL flag = [self.searcher reverseGeoCode:reverseGeoCodeSearchOption];
if(flag)
{
NSLog(@"反geo檢索發送成功");
}
else
{
NSLog(@"反geo檢索發送失敗");
}
});
}
}
#pragma mark - onGetReverseGeoCodeResult(反向地理編碼結果)
//接收反向地理編碼結果
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
//這裡列印出反向地理編碼的結果,包括城市,地址等資訊
NSLog(@"測試結果 %@ %@",result.addressDetail.city,result.address);
_placename.text = result.address;
}
else {
NSLog(@"抱歉,未找到結果");
}
}
#pragma mark - configDelegate(設定代理及取消代理)
//設定代理
-(void)viewWillAppear:(BOOL)animated
{
[mapView viewWillAppear];
mapView.delegate = self;
}
// 此處記得不用的時候需要置nil,否則影響記憶體的釋放
-(void)viewWillDisappear:(BOOL)animated
{
[mapView viewWillDisappear];
// mapView.delegate = nil; // 不用時,置nil
_searcher.delegate = nil;
}
#pragma mark - checkNetwork(檢查網路及授權)
//檢查網路狀態
- (void)onGetNetworkState:(int)iError
{
if (0 == iError) {
NSLog(@"連網成功");
}
else{
NSLog(@"onGetNetworkState %d",iError);
}
}
#pragma mark - BMKMapViewDelegate(地表徵圖注及路線顏色)
/**
*根據overlay產生對應的View
*@param mapView 地圖View
*@param overlay 指定的overlay
*@return 產生的覆蓋物View
*/
- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]]) {
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:1];
polylineView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.7];
polylineView.lineWidth = 3.0;
return polylineView;
}
return nil;
}
//開啟跟隨
mapView.userTrackingMode = BMKUserTrackingModeFollow;
mapView.showsUserLocation = YES;//顯示定位元影像層
寫的不是很好 給自己做個儲存作用
iOS成長之路 百度地圖