使用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