iOS開發(fā)(第三方使用)——百度地圖的簡單使用(定位與當(dāng)前位置的顯示)

使用cocoapods導(dǎo)入 pod ‘BaiduMapKit’

在plist添加NSLocationAlwaysUsageDescription

去百度地圖開發(fā)者中心注冊帳號,并創(chuàng)建項目,拿到AK的值

在工程的AppDelegate.m導(dǎo)入頭文件BaiduMapAPI_Base/BMKBaseComponent.h、BaiduMapAPI_Map/BMKMapComponent.h

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //百度地圖 BMKMapManager *_mapManager = [[BMKMapManager alloc]init]; BOOL ret = [_mapManager start:@"你的AK" generalDelegate:self]; if (!ret) { NSLog(@"manager start failed!"); } return YES; }

在控制器里面導(dǎo)入頭文件BaiduMapAPI_Location/BMKLocationService.h、BaiduMapAPI_Map/BMKMapComponent.h并設(shè)置代理BMKLocationServiceDelegate,BMKMapViewDelegate

`- (void)viewDidLoad {
[super viewDidLoad];
//初始化BMKLocationService
_locService = [[BMKLocationService alloc]init];
_locService.delegate = self;
//啟動LocationService
[_locService startUserLocationService];

_mapView=[[BMKMapView alloc] initWithFrame:self.view.frame];
_mapView.delegate = self; // 此處記得不用的時候需要置nil,否則影響內(nèi)存的釋放
_mapView.mapType = BMKMapTypeStandard;
[_mapView setZoomLevel:19.0];
[self.view addSubview:_mapView];
}

(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
BMKCoordinateRegion region;
region.center.latitude = userLocation.location.coordinate.latitude;
region.center.longitude = userLocation.location.coordinate.longitude;

region.span.latitudeDelta = 0.2;
region.span.longitudeDelta = 0.2;
if (_mapView)
{
_mapView.region = region;

}

[_mapView setZoomLevel:19.0];

[_locService stopUserLocationService];//定位完成停止位置更新

//添加當(dāng)前位置的標(biāo)注
CLLocationCoordinate2D coord;
coord.latitude = userLocation.location.coordinate.latitude;
coord.longitude = userLocation.location.coordinate.longitude;
BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc] init];
_pointAnnotation.coordinate = coord;

CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
pt=(CLLocationCoordinate2D){coord.latitude,coord.longitude};

dispatch_async(dispatch_get_main_queue(), ^{
[_mapView removeOverlays:_mapView.overlays];
[_mapView setCenterCoordinate:coord animated:true];
[_mapView addAnnotation:_pointAnnotation];

});
}
收錄:https://blog.csdn.net/liumude123/article/details/52243399

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容