iOS开发(第三方使用)——百度地图的简单使用(定位与当前位置的显示)

iOS开发交流群:301058503
1. 使用cocoapods导入 pod ‘BaiduMapKit’(不会使用cocoapods的朋友可以参考http://blog.csdn.net/liumude123/article/details/51405253)
2. 在plist添加NSLocationAlwaysUsageDescription
3. 去百度地图开发者中心注册帐号,并创建项目,拿到AK的值
4. 在工程的AppDelegate.m导入头文件BaiduMapAPI_Base/BMKBaseComponent.h、BaiduMapAPI_Map/BMKMapComponent.h
5. - (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;
}

6. 在控制器里面导入头文件BaiduMapAPI_Location/BMKLocationService.h、BaiduMapAPI_Map/BMKMapComponent.h并设置代理BMKLocationServiceDelegate,BMKMapViewDelegate
7. `- (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,否则影响内存的释放
_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];//定位完成停止位置更新

    //添加当前位置的标注
    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];

    });
    }

iOS开发交流群:301058503

猜你喜欢

转载自blog.csdn.net/liumude123/article/details/52243399