美文网首页ios地图iOS开发·地图与定位篇iOS技术相关
百度地图集成_集成SDK和基本功能实现

百度地图集成_集成SDK和基本功能实现

作者: 小白哥有话说 | 来源:发表于2016-11-08 17:39 被阅读399次

    1.百度地图导入项目中。。。。

    1.注册百度地图,成为其开发者

    注册百度地图开发者

    2.创建应用:申请获得密匙

    创建应用获得密匙

    3.下载相关Demo

    下载相关Demo

    4.打开对应的demo,把BoundleId替换成注册的密匙,然后再把对应的key替换AppDelegate中的输入你的key,之后运行Dem哦,成功

    自己项目中集成为:

    在自己项目中集成

    按照其中的手动配置.framework形式开发包要求做即可,如此大功告成(其中的要把任意一个文件变为.mm文件,不这样做也是可以的(Xcode8))

    如何集成地图基本功能:

    首先在AppDelegate中程序一起动打开百度地图代码如下:

    - (BOOL)application:(UIApplication *)application

    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      // 要使用百度地图,请先启动BaiduMapManager

    _mapManager = [[BMKMapManager alloc]init];

    // 如果要关注网络及授权验证事件,请设定    generalDelegate参数

    BOOL ret = [_mapManager start:@"替换成您的key"  generalDelegate:nil];

    if (!ret) {

    NSLog(@"manager start failed!");

    }

    // Add the navigation controller's view to the window and display.

    self.window.frame =[UIScreen mainScreen].bounds;

    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:[[BMKMainListViewController alloc] init]];

    self.window.rootViewController =nav;

    [self.window makeKeyAndVisible];

    return YES;

    }

    其中下面的2个方法要遵循BMKGeneralDelegate,实现才有用的

    /**

    *返回网络错误

    *@param iError 错误号

    */

    - (void)onGetNetworkState:(int)iError {

    if (iError) {

    NSLog(@"error =%d",iError);

    mAlertView(@"提示", @"联网失败");

    }else {

    NSLog(@"联网成功");

    }

    }

    /**

    *返回授权验证错误

    *@param iError 错误号 : 为0时验证通过,具体参加BMKPermissionCheckResultCode

    */

    - (void)onGetPermissionState:(int)iError {

    if (iError) {

    mAlertView(@"提示", @"授权失败");

    NSLog(@"error =%d",iError);

    }else {

    mAlertView(@"提示", @"授权成功");

    NSLog(@"授权成功");

    }

    }

    在需要集成的VC中,

    1.首先写入下面的代码(为了防止内存无法释放的问题)

    -(void)viewWillAppear:(BOOL)animated {

    [self.mapView viewWillAppear];

    self.mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

    [BMKMapView enableCustomMapStyle:self.isAutoMap];

    }

    -(void)viewWillDisappear:(BOOL)animated {

    [BMKMapView enableCustomMapStyle:NO];//关闭个性化地图

    [self.mapView viewWillDisappear];

    self.mapView.delegate = nil; // 不用时,置nil

    }

    -(void)dealloc {

    if (_mapView) {

    _mapView =nil;

    }

    }

    2.MapView中如何想更改地图的展示效果,可以导入对应的文件配置,写入以下代码

    + (void)initialize {

    // 设置必须在BMKMapView初始化之前(会影响所有地图示例)

    NSString *path =[[NSBundle mainBundle] pathForResource:@"custom_config_黑夜" ofType:@""];

    [BMKMapView customMapStyle:path];

    }

    在需要改变属性的地方调用

    [BMKMapView enableCustomMapStyle:self.isAutoMap];

    其中self.isAutoMap为no时,是正常模式,为yes为自定义的某种模式

    3.实现BMKMapViewDelegate的以下代理方法:

    - (void)mapViewDidFinishLoading:(BMKMapView *)mapView {

    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"BMKMapView控件初始化完成" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];

    //    [alert show];

    //    alert = nil;

    }

    - (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate {

    NSLog(@"map view: click blank");

    }

    - (void)mapview:(BMKMapView *)mapView onDoubleClick:(CLLocationCoordinate2D)coordinate {

    NSLog(@"map view: double click");

    }

    附上:      本人项目地址:

    附上:    2.百度地图定位篇

    相关文章

      网友评论

        本文标题:百度地图集成_集成SDK和基本功能实现

        本文链接:https://www.haomeiwen.com/subject/kuaauttx.html