百度地图的集成和简单使用

来源:互联网 发布:usb c电源适配端口 编辑:程序博客网 时间:2024/06/04 18:49

在iOS开发中会遇到需要使用地图显示需要展示的地理位置的情况,这里使用的是百度地图SDK,高德地图也大同小异。


首先,我们去百度地图的官网上了解一下集成的介绍,下载SDK,没有注册开发者的注册一下,就可以提取APPKey密钥了。



按照开发文档的操作导入相关SDK的库类和依赖库,引入mapapi.bundle资源文件,从BaiduMapAPI_Map.framework||Resources文件中选择mapapi.bundle文件导入到工程中,因为导入库类时并没有把这个图片资源移入进去,反正按文档来就对了。

把工程中的一个类后缀改为.mm混合编译,这里我们把APPDelegate.m文件改了。


按文档配置好开发环境后,就开始写代码了。

在APPDelegate文件中,

引入头文件

#import <BaiduMapAPI_Base/BMKMapManager.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch..


    

    

    //======== 百度地图 ======================

    BMKMapManager * mapManager = [[BMKMapManager alloc] init];

    BOOL result = [mapManager start:@"你申请的APPKey" generalDelegate:nil];

    if (!result) {

        NSLog(@"百度引擎启动失败!");

    }

 

    return YES;

}


这样百度地图引擎就开启了。

然后在你需要打开百度地图的viewController写相关代码,

这里我建立了一个MapShowViewController的类,在.m文件中写相关需要实现的功能代码。我简单的实现了把几个坐标用大头针标注在地图上的功能,并且定位了手机当前的位置。


效果如下,




#import "MapShowViewController.h"

#import <BaiduMapAPI_Map/BMKMapView.h>

#import <BaiduMapAPI_Map/BMKPointAnnotation.h>

#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>

#import <BaiduMapAPI_Location/BMKLocationService.h>


@interface MapShowViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>

{

    BMKMapView * mapView;

    BMKLocationService * locService;

    NSMutableArray * venuesLocationArray;

}

@end


@implementation MapShowViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.navigationItem.title = @"附近场馆";

    self.automaticallyAdjustsScrollViewInsets = YES;

    [self initInterFace];

    venuesLocationArray = [[NSMutableArray alloc] init];

    [venuesLocationArray setArray:@[@{@"lat":@"22.536288",@"lnt":@"114.039077",@"name":@"罗湖高尔夫"},

                                   @{@"lat":@"22.575744477169",@"lnt":@"114.04361926289",@"name":@"深圳高尔夫"}]];

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [mapView viewWillAppear];

    locService.delegate = self;

    mapView.delegate = self;

}


- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

    for (int i = 0; i < venuesLocationArray.count; i++) {

        

        BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];

        CLLocationCoordinate2D coor;

        coor.latitude = [venuesLocationArray[i][@"lat"] doubleValue];

        coor.longitude = [venuesLocationArray[i][@"lnt"] doubleValue];

        annotation.coordinate = coor;

        annotation.title = venuesLocationArray[i][@"name"];


        [mapView addAnnotation:annotation];

    }

    

}


- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    [mapView viewWillDisappear];

    locService.delegate = nil;

    mapView.delegate = nil;

}



- (void)initInterFace{


    //初始化定位 BMKLocationService

    locService = [[BMKLocationService alloc]init];

    locService.distanceFilter = 200;//设定定位的最小更新距离,这里设置 200m定位一次,频繁定位会增加耗电量

    locService.desiredAccuracy = kCLLocationAccuracyHundredMeters;//设定定位精度

    //启动LocationService

    [locService startUserLocationService];

    

    //初始化地图

    mapView = [[BMKMapView alloc] initWithFrame:self.view.frame];

    mapView.showMapScaleBar = YES; // 设定是否显式比例尺

    mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态

    mapView.zoomLevel = 14;

    mapView.showsUserLocation = YES;

    self.view = mapView;

    

}


#pragma mark - MapView协议方法

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        newAnnotationView.pinColor = BMKPinAnnotationColorGreen;

        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示

//        newAnnotationView.image = [UIImage imageNamed:@"locate.png"];//设置大头针图片

        return newAnnotationView;

    }

    return nil;


}


#pragma mark - location协议方法

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {

    [mapView updateLocationData:userLocation];// 动态更新我的位置数据

    [mapView setCenterCoordinate:userLocation.location.coordinate];//当前地图的中心点

}





@end



功能暂时只做了这些,如有不足请多见谅。





0 0
原创粉丝点击