iOS開發之百度地圖的集成——地圖標註&POI檢索

来源:http://www.cnblogs.com/Jepson1218/archive/2016/03/17/5288287.html
-Advertisement-
Play Games

本篇分為兩部分: 第一步:首先創建 BMKMapView 視圖 第二步:在視圖完全顯示出來後設置,並實現代理方法 第三步:運行程式,此時大頭針效果可以正常顯示 第一步:延時載入對象 第二步:實現BMKPoiSearchDelegate代理方法 第三步:實現 BMKPoiSearchDelegate


本篇分為兩部分:

一、地圖標註

  第一步:首先創建 BMKMapView 視圖

  第二步:在視圖完全顯示出來後設置,並實現代理方法

  第三步:運行程式,此時大頭針效果可以正常顯示

二、POI檢索

  第一步:延時載入對象

  第二步:實現BMKPoiSearchDelegate代理方法

  第三步:實現 BMKPoiSearchDelegate 處理回調結果

  第四步:運行程式,此時便可檢索到附近小吃相關標註


一、地圖標註

標註BMKAnnotation一定要實現為標註對應的protocal<BMKMapViewDelegate>

第一步:首先創建 BMKMapView 視圖

- (BMKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];
        self.view = _mapView;
    }
    return _mapView;
}

 第二步:在視圖完全顯示出來後設置,並實現代理方法

- (void) viewDidAppear:(BOOL)animated {
    // 添加一個PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"這裡是北京";
    annotation.subtitle = @"我為你無法呼吸~";
    [_mapView addAnnotation:annotation];
}

// 自定義添加大頭針方法
- (void)addAnnoWithPT:(CLLocationCoordinate2D)coor andTitle:(NSString *)title andAddress:(NSString *)address {
    // 添加一個PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    annotation.coordinate = coor;
    annotation.title = title;
    annotation.subtitle = address;
    [_mapView addAnnotation:annotation];
}
#pragma mark
#pragma mark - BMKLocationServiceDelegate 代理方法,用於添加大頭針
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation {
    static NSString *identifier = @"myAnnotation";
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (!newAnnotationView) {
            newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        }
        newAnnotationView.annotation = annotation;
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 設置該標註點動畫顯示
        
        //添加按鈕監聽點擊事件
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
        newAnnotationView.rightCalloutAccessoryView = btn;
        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        
        return newAnnotationView;
    }
    return nil;
}

 

 第三步:運行程式,此時大頭針效果可以正常顯示

 


二、POI檢索

第一步:延時載入對象

- (BMKPoiSearch *)poiSearch {
    if (!_poiSearch) {
        _poiSearch = [[BMKPoiSearch alloc] init];
        _poiSearch.delegate = self;
    }
    return _poiSearch;
}

第二步:實現BMKPoiSearchDelegate代理方法

// 長按地圖時會調用此方法
- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate {
    //發起檢索
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = 0;
    option.pageCapacity = 20;
    option.location = coor;
    option.keyword = @"小吃";
    BOOL flag = [self.poiSearch poiSearchNearBy:option];
    if(flag) {
        NSLog(@"周邊檢索發送成功");
    } else {
        NSLog(@"周邊檢索發送失敗");
    }
    
    // 設置初始化區域
    CLLocationCoordinate2D center = option.location;
    BMKCoordinateSpan span;
    span.latitudeDelta = 0.016263;
    span.longitudeDelta = 0.012334;
    BMKCoordinateRegion region;
    region.center = center;
    region.span = span;
    [self.mapView setRegion:region animated:YES];
}

 第三步:實現 BMKPoiSearchDelegate 處理回調結果

- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此處理正常結果
//        NSLog(@"成功:%@", poiResultList.poiInfoList);
        
        [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSLog(@"%@----%@", obj.name, obj.address);  // 由於設置檢索時,每頁指定了10條,所以此處檢索出10條相關信息
            [self addAnnoWithPT:obj.pt andTitle:obj.name andAddress:obj.address];
        }];
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //當在設置城市未找到結果,但在其他城市找到結果時,回調建議檢索城市列表
        // result.cityList;
        NSLog(@"起始點有歧義");
    } else {
        NSLog(@"抱歉,未找到結果, %zd", error);
    }
}

 第四步:運行程式,此時便可檢索到附近小吃相關標註

 註意:需要引入的頭文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的頭文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本篇主要講述百度地圖的導航功能: 第一步:在使用百度導航之前,我們需要在百度地圖開放平臺上下載導航的 SDK,共85.8M,網速不好的同學可提前準備好。 第二步:引入導航所需的系統包 將AudioToolbox.framework、ImageIO.framework、CoreMotion.frame
  • 1.SQLite資料庫: SQLite 是一個開源的嵌入式關係資料庫,實現自包容、零配置、支持事務的SQL資料庫引擎。 其特點是高度便攜、使 用方便、結構緊湊、高效、可靠。 與其他資料庫管理系統不同,SQLite 的安裝和運行非常簡單,在大多數情況下 - 只要確保 SQLite的二進位文件存在即可開
  • 一般白色就是0,黑色就是1 單色點陣圖: 24位點陣圖 256色 安卓中預設使用32位的 將一個圖片放在SD卡上,使用BitmapFactory.decodeFile解析得Bitmap設置ImageView顯示 以上的圖片可以正常載入 異常現象 載入 的圖片過大,如2560*1520之類的,載入 時就會
  • 本文主要實現了微信的個人主頁的設置: 目錄文件如下: 實現代碼如下: RootTableViewController.h RootTableViewController.m AppDelegate.h AppDelegate.m 效果圖如下:
  • 主要在清單文件這樣配置: 2、那麼如何讓屏幕橫屏或者豎屏時對activity沒有影響呢?應當這樣配置:
  • 我們先看一下效果 代碼如下 首先是第一個頁面 rootTableViewController.h rootTableViewController.m 第二個頁面 cityTableViewController.h 第二個頁面 cityTableViewController.m 指定根目錄AppDel
  • 網路請求預設是get 網路請求有很多種:GET查 POST改 PUT增 DELETE刪 HEAD 在平時開發中主要用的 是 get 和 post. get 獲得數據 (獲取用戶信息) get 請求是沒有長度限制的,真正的長度限制是瀏覽器做的,限制長度一般2k get 請求是有緩存的,get 有冪等的
  • 字典 NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"One", @"1", @"Two", @"2", @"Three", @"3", @"One", @"4", nil]; //字典中的數據以鍵值對的方式進
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...