美文网首页
百度地图使用笔记

百度地图使用笔记

作者: Karen_ | 来源:发表于2015-12-08 10:25 被阅读183次
    创建地图
    1. 创建开发者账号
    2. 创建应用 申请秘钥
    3. 在工程中将ViewController.mm 因为SDK有一部分是以C++写的

    静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即将Xcode的Project -> Edit Active Target -> Build -> GCC4.2 - Language -> Compile Sources As设置为"Objective-C++"

    ViewController
    1. 修改plis文件
      在plist文件中添加
      • NSLocationAlwaysUsageDescription
      • NSLocationWhenInUseUsageDescription
      • BaiduNew
    plist
    1. 配置环境

    导入百度提供的.framework包

    添加包

    引入所需的系统库
    百度地图SDK中提供了定位功能和动画效果,v2.0.0版本开始使用OpenGL渲染,因此您需要在您的Xcode工程中引入CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework、libsqlite3.0.tbd(xcode7以前为 libsqlite3.0.dylib)、CoreTelephony.framework 、libstdc++.6.0.9.tbd(xcode7以前为libstdc++.6.0.9.dylib)。
    (注:加粗标识的系统库为v2.9.0新增的系统库,使用v2.9.0及以上版本的地图SDK,务必增加导入这3个系统库。)

    最后

    在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。
    ** 注意大小写**


    -ObjC
    1. 引入mapapi.bundle资源文件
      方法:选中工程名,在右键菜单中选择Add Files to “工程名”…,从BaiduMapAPI_Map.framework||Resources文件中选择mapapi.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中。
    屏幕快照 2015-12-08 09.58.39.png 屏幕快照 2015-12-08 09.58.45.png 屏幕快照 2015-12-08 09.58.49.png
    1. 添加头文件

    import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
    import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

    AppDelegate

    添加属性
    @property (nonatomic, strong)BMKMapManager *manager;
    签协议
    <BMKGeneralDelegate>
    初始化

    - (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 addSubview:navigationController.view];  
        [self.window makeKeyAndVisible];  
       return YES;  
    }
    

    实现协议方法

    /**
     *  获取网络状态
     */
    -(void)onGetNetworkState:(int)iError {
    
        if (iError == 0) {
            NSLog(@"联网成功");
        } else {
            NSLog(@"联网失败");
        }
    
    }
    /**
     *  获取授权状态
     */
    -(void)onGetPermissionState:(int)iError {
    
        if (iError == 0) {
            NSLog(@"获取授权成功");
        } else {
            NSLog(@"获取授权失败");
        }
    
    }
    
    • 进入后台 进入前台
    - (void)applicationWillResignActive:(UIApplication *)application {
    
        [BMKMapView willBackGround];
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application {
    
        [BMKMapView didForeGround];
    }
    
    • 在storyBoard 添加NavigationController


      屏幕快照 2015-12-08 10.13.02.png

    添加一个View

    屏幕快照 2015-12-08 10.15.06.png

    使其继承于BMKMaoView

    屏幕快照 2015-12-08 10.15.42.png

    这时候运行一下


    屏幕快照 2015-12-08 10.17.04.png
    添加功能
    • 在之前的view上添加Button 让其显示在View上
    • 引入头文件 拖拽view添加属性
    #import "ViewController.h"
    #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
    #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet BMKMapView *mapView;
    
    @end
    
    • 签代理 <BMKMapViewDelegate>
    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        self.mapView.delegate = self;
    }
    
    -(void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        self.mapView.delegate = nil;
    
    }
    
    • view 上再添加一个View 黑色 Alpha0.6

    • 拖拽Button和灰色View添加属性


      如图
    • button 添加点击事件

    - (IBAction)showSView:(id)sender {
        NSLog(@"SDFSDFAS");
        if (self.sView.alpha == 0) {
            [UIView animateWithDuration:0.2 animations:^{
                self.sView.alpha = 0.6;
            }];
        } else {
            [UIView animateWithDuration:0.2 animations:^{
                self.sView.alpha = 0;
            }];
        }
    
    }
    
    • 添加label 和 switch
    屏幕快照 2015-12-08 11.34.01.png
    • 卫星地图switch点击事件
    - (IBAction)switchMapType:(id)sender {
    
        UISwitch *mySwitch = (UISwitch *)sender;
    
        [self.mapView setMapType:mySwitch.on == YES ? BMKMapTypeSatellite : BMKMapTypeStandard];
    
    }
    
    • 添加实时路况同上:


      屏幕快照 2015-12-08 11.45.26.png

    实时了路况点击事件:

    - (IBAction)switchMapLoade:(id)sender {
    
        UISwitch *mySwitch = (UISwitch *)sender;
    
        self.mapView.trafficEnabled = mySwitch.on;
    
    }
    
    • 添加热力图


      热力图
    - (IBAction)showHeat:(id)sender {
    
        UISwitch *mySwitch = (UISwitch *)sender;
    
        self.mapView.baiduHeatMapEnabled = mySwitch.on;
    
    }
    
    • 添加四个Button 分别拖拽为属性 如图


      添加Button

      在viewDidLoad里隐藏上面三个Button

      self.followButton.alpha = 0;
        self.headButton.alpha = 0;
        self.endButton.alpha = 0;
    
    • 给开始定位按钮添加点击事件 使上面三个Button出现开始定位按钮隐藏
    - (IBAction)startLocation:(id)sender {
    
    
            [UIView animateWithDuration:0.3 animations:^{
                self.startButton.alpha = 0;
                self.followButton.alpha = 1;
                self.headButton.alpha = 1;
                self.endButton.alpha = 1;
                self.sView.alpha = 0;
            }];
    
    }
    
    • 引入定位功能的头文件
      #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
    • 添加属性
      @property (nonatomic, strong) BMKLocationService *locationService;
      *签代理
      BMKLocationServiceDelegate
      *初始化
      self.locationService = [[BMKLocationService alloc] init];
    • 签代理
      self.locationService.delegate =self;
    • 在开始定位时启动本地定位 开始定位的点击事件里添加
      [self.locationService startUserLocationService]; [self.mapView showsUserLocation];
    • 在关闭定位的点击事件中关闭定位
      [self.locationService stopUserLocationService]; self.mapView.showsUserLocation = NO;
    • 添加代理方法
    /**
     * 地图更新
     */
    -(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
        [self.mapView updateLocationData:userLocation];
    }
    

    未完待续.......

    相关文章

      网友评论

          本文标题:百度地图使用笔记

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