百度地图使用笔记

2015-12-08  本文已影响183人  Karen_
创建地图
  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];
}

添加一个View

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

使其继承于BMKMaoView

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

这时候运行一下


屏幕快照 2015-12-08 10.17.04.png
添加功能
#import "ViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
@interface ViewController ()
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

@end
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.mapView.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.mapView.delegate = nil;

}
- (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;
        }];
    }

}
屏幕快照 2015-12-08 11.34.01.png
- (IBAction)switchMapType:(id)sender {

    UISwitch *mySwitch = (UISwitch *)sender;

    [self.mapView setMapType:mySwitch.on == YES ? BMKMapTypeSatellite : BMKMapTypeStandard];

}

实时了路况点击事件:

- (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;

}
  self.followButton.alpha = 0;
    self.headButton.alpha = 0;
    self.endButton.alpha = 0;
- (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;
        }];

}
/**
 * 地图更新
 */
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    [self.mapView updateLocationData:userLocation];
}

未完待续.......

上一篇 下一篇

猜你喜欢

热点阅读