zoukankan      html  css  js  c++  java
  • (八十四)集成百度地图和基本使用

    配置的具体内容能够查看百度地图API文档来获取。

    ①首先下载百度iOS SDK的完整包,将当中Lib的Release-iphonesimulator下的framework文件导入到project,然后双击打开framework文件。再打开Resources,将当中的mapapi.bundle导入到project。

    ②从系统设置中导入下列框架:

    ③在Build Settings里搜索other linker Flags,加入-ObjC标记。

    ④由于静态库用到了C++。因此project中必须有mm文件,最简单的方法是把当中一个m文件改成mm。

    ⑤对于Xcode6和以后的版本号,还须要在info.plist中增加Bundle display name,值随便写。一般写Bundle identifier的值。

    ⑥在AppDelegate中创建管理者。然后启动,须要传入key。

    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @property (nonatomic, strong) BMKMapManager *mapManager;
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        _mapManager = [[BMKMapManager alloc] init];
        BOOL ret = [_mapManager start:@"这里填写key" generalDelegate:self];
        if (!ret) {
            NSLog(@"manager start failed");
        }
        
        return YES;
    }
    
    
    @end

    Tip:key和bundle identify的值应该和申请开发人员时创建的应用内的一致。否则无法正常的授权。

    ⑦显示地图和基本使用

    以下的代码创建了MapView和Poisearch。实现了地图的显示和POI检索。

    #import "ViewController.h"
    #import <BaiduMapAPI/BMapKit.h>
    
    @interface ViewController () <BMKMapViewDelegate,BMKPoiSearchDelegate>
    
    @property (nonatomic, weak) BMKMapView *mapView;
    @property (nonatomic, strong) BMKPoiSearch *searcher;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 20, 320, 420)];
        [self.view addSubview:mapView];
        _mapView  = mapView;
        
        _searcher = [[BMKPoiSearch alloc] init];
        _searcher.delegate = self;
        
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [_mapView viewWillAppear];
        _mapView.delegate = self; // 此处记得不用的时候须要置nil。否则影响内存的释放
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [_mapView viewWillDisappear];
        _mapView.delegate = nil; // 不用时,置nil
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        
        NSLog(@"in");
        
        // 检索周边信息
        BMKCitySearchOption *option = [[BMKCitySearchOption alloc] init];
        option.pageIndex = 0;
        option.pageCapacity = 10;
        option.city = @"北京";
        option.keyword = @"小吃";
        BOOL flag = [_searcher poiSearchInCity:option];
        if(flag)
        {
            NSLog(@"周边检索发送成功");
        }
        else
        {
            NSLog(@"周边检索发送失败");
        }
    }
    
    - (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
    {
        // 清楚屏幕中全部的annotation
        NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
        [_mapView removeAnnotations:array];
        
        if (error == BMK_SEARCH_NO_ERROR) {
            NSMutableArray *annotations = [NSMutableArray array];
            for (int i = 0; i < result.poiInfoList.count; i++) {
                BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
                BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
                item.coordinate = poi.pt;
                item.title = poi.name;
                [annotations addObject:item];
            }
            [_mapView addAnnotations:annotations];
            [_mapView showAnnotations:annotations animated:YES];
        } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
            NSLog(@"起始点有歧义");
        } else {
            // 各种情况的推断。

    。。 } } @end

  • 相关阅读:
    iframe自动适应高度
    php正则过滤html标签、空格、换行符的代码,提取图片
    destoon使用中的一些心得
    Fiddler 过滤 css,图片等请求url 正则表达式
    不用递归实现无限分类数据的树形格式化
    PHP定时执行任务的实现
    Discuz! X3.1去除内置门户导航/portal.php尾巴的方法
    discuzx完全自定义设计模板门户首页,栏目,专题模板方法
    Apache环境.htaccess伪静态301跳转(www与不带www)
    15万甚至30万以内的SUV值不值得买?
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6979401.html
Copyright © 2011-2022 走看看