美文网首页iOS程序猿iOS Developer
百度地图SDK学习——1.0 显示地图

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

作者: playman | 来源:发表于2016-08-28 14:59 被阅读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
    

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

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

    相关文章

      网友评论

        本文标题:百度地图SDK学习——1.0 显示地图

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