百度地图SDK学习——1.0 显示地图

申请账号

百度SDK官网

http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5

创建应用

官方步骤

http://lbsyun.baidu.com/index.php?title=iossdk

1、申请key

获取秘钥

2、创建新的应用

创建应用

3、填写信息

填写应用信息

4、获得key

获得key

安装pod

由于我是通过pod进行集成百度地图,所以要先安装pod
如果是碰到ruby升级<=2.2.2的问题的朋友,可以装完rvm之后执行这条命令

$gem sources --remove https://rubygems.org/
运行完这行命令,继续
$ gem sources -a https://gems.ruby-china.org
$sudo gem install -n /usr/local/bin cocoapods --pre
把master内容解压到/Users/你的用户名/.cocoapods/repos路径下

master内容是cocoapodsmaster.zip。日后云盘文件添加

创建工程

官方教程http://lbsyun.baidu.com/index.php?title=iossdk/guide/attention

不用故事版创建

1、正常创建工程

2、修改plist内容

<key>NSAppTransportSecurity</key> 
 <dict> 
  <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>LSApplicationQueriesSchemes</key> 
  <array> 
   <string>baidumap</string> 
 </array>
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
二选一添加
Bundle display name
添加内容

3、将AppDelegate.m改为AppDelegate.mm

4、添加Podfile

platform :ios, "8.0"
pod 'BaiduMapKit'
target :'项目名' do
end
pod install
等待一会,打开workspace文件。就集成好了。

5、准备让地图显示到view

  • 修改默认的main
修改默认的main
  • AppDelegate.h
#import <UIKit/UIKit.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, BMKGeneralDelegate>
{
    UINavigationController *navigationController;
    BMKMapManager* _mapManager;
}
@property (strong, nonatomic) UIWindow *window;

@end
  • AppDelegate.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    _mapManager = [[BMKMapManager alloc]init];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor orangeColor];
    
    BOOL ret = [_mapManager start:@"自己的key"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    ViewController * vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;

    [self.window makeKeyAndVisible];
    
    return YES;
}
  • ViewController.m
#import "ViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#include <BaiduMapAPI_Map/BMKMapView.h>

@interface ViewController ()<BMKMapViewDelegate>
@property(nonatomic, strong)BMKMapView* mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    self.view = _mapView;
}

-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时,置nil
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

运行

最终结果图

用storyboard模式使用

之前步骤一样,只不过不使用AppDelegate,里面不写东西了。
直接在ViewController中写内容,同样能显示
#import "ViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>

@interface ViewController ()

@property(nonatomic, strong)BMKMapManager * mapManager;
@property(nonatomic, strong)BMKMapView * mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    _mapManager = [[BMKMapManager alloc] init];
    BOOL result = [_mapManager start:@"emMQtEQNOkLCXA4e6dbE5ZKshb94sGkN" generalDelegate:nil];
    if (!result) {
        NSLog(@"Failed");
    }
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-20)];
    [self.view addSubview:_mapView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

文档内容的细节需要自己多看看,按这个写下来是可以显示的,我也不懂官方文档究竟写了个啥!!!

之后的细节内容学习之后再来补充。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各位小伙伴们大家好,今天我向大家介绍一下苹果百度地图的使用方法,因为做过一些想关的APP,感觉百度地图还是挺方便的...
    Lee0528阅读 14,784评论 18 46
  • CocoaPods 是什么? CocoaPods 是一个负责管理 iOS 项目中第三方开源库的工具。CocoaPo...
    朝洋阅读 25,754评论 3 50
  • 1.申请密钥 首先,申请一个baidu账号,接着进入新建密钥入口申请成为baidu地图开发者,填写相关开发者信息和...
    小码僧阅读 17,690评论 7 143
  • 我想去一个地方 那里没有忧伤 山谷里飘满紫罗兰的清香 风声中荡漾着轻轻的吟唱 爱与美诉说着永恒 光与影舞动如霓裳 ...
    prudencer阅读 289评论 4 3
  • 深夜,我看见微信朋友圈初表姐发了几张她获得北京市教育委员会颁发的英语口语三等奖比赛证书的照片,替她高兴自豪的同时不...
    木棉花开在橡树旁阅读 606评论 0 0