本人有若干成套學(xué)習(xí)視頻, 可試看! 可試看! 可試看, 重要的事情說三遍 包含Java
, 數(shù)據(jù)結(jié)構(gòu)與算法
, iOS
, 安卓
, python
, flutter
等等, 如有需要, 聯(lián)系微信tsaievan
.
POI = point of interest (興趣點(diǎn))
興趣點(diǎn)檢索應(yīng)該是地圖類 SDK 最重要的接口了, 也是電商類 , 打車類APP 必備的功能,那么興趣點(diǎn)檢索的功能如何完成呢?
先看一下思路圖:
POI Search思路圖
讓我們來 step by step 吧
- 在 BaiduMapManager 類中設(shè)置 BMKPoiSearch 屬性,并對其進(jìn)行懶加載初始化
// -------- POI 興趣點(diǎn)查找 --------
- (BMKPoiSearch *)poiSearch
{
if (!_poiSearch) {
_poiSearch = [[BMKPoiSearch alloc]init];
}
return _poiSearch;
}
- 創(chuàng)建 poiSearch分類,并將處理POI Search 的邏輯寫在這個分類里
// -------- 根據(jù)關(guān)鍵詞查詢興趣點(diǎn) --------
- (void)poiSearchWithKeyword:(NSString *)keyword
{
kBaiduMapManager.poiSearch.delegate = self;
/* 初始化查詢請求 */
BMKNearbySearchOption *searchOption = [[BMKNearbySearchOption alloc]init];
/* 設(shè)置查詢請求的屬性 */
/* note : 關(guān)鍵詞一定要設(shè)置,不然代理的 API 不走 */
searchOption.keyword = keyword;
searchOption.location = kBaiduMapManager.locationService.userLocation.location.coordinate;
searchOption.radius = 1000;
/* 執(zhí)行查詢請求 */
[kBaiduMapManager.poiSearch poiSearchNearBy:searchOption];
}
- 暴露一個接口,供外界使用, 在這個 demo 中,我使用的是 searchBar, 當(dāng)我點(diǎn)擊 searchBar 自動彈出鍵盤, 點(diǎn)擊"search/ 查找"鍵時,自動根據(jù)搜索框的文本進(jìn)行查找
#pragma mark *** UISearchBarDelegate 代理方法 ***
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
/* 根據(jù) searchBar 的關(guān)鍵字搜索 */
[kBaiduMapManager poiSearchWithKeyword:searchBar.text];
}
- 查詢結(jié)果代理 API 回調(diào),將回調(diào)結(jié)果轉(zhuǎn)換成 Annotation 模型
#pragma mark *** BMKPoiSearchDelegate API 回調(diào) ***
/* 設(shè)置大頭針模型緩存 */
NSMutableArray *annotationsCache = nil;
// -------- POI 查詢結(jié)果回調(diào) --------
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
/* 在顯示新的查詢結(jié)果之前清空緩存 */
[kBaiduMapManager.mapView removeAnnotations:annotationsCache];
annotationsCache = [NSMutableArray array];
NSArray <BMKPoiInfo *> *infoArray = poiResult.poiInfoList;
for (BMKPoiInfo *info in infoArray) {
/* 將查詢結(jié)果信息轉(zhuǎn)化為大頭針模型 */
YFAnnotation *annotation = [[YFAnnotation alloc]init];
annotation.coordinate = info.pt;
annotation.title = info.name;
[annotationsCache addObject:annotation];
}
/* 將大頭針添加在視圖上 */
[kBaiduMapManager.mapView addAnnotations:annotationsCache];
}
- 自定義 YFAnnotationView 視圖,繼承自 BMKAnnotationView
#import "YFAnnotationView.h"
static NSString *reuseIdentifier = @"YFAnnotationViewReuseIdentifier";
@implementation YFAnnotationView
// -------- 自定義構(gòu)造方法返回大頭針視圖 --------
+ (instancetype)annotationViewWithAnnotation:(YFAnnotation *)annotation andMapView:(BMKMapView *)mapView
{
/* 已創(chuàng)建好大頭針視圖時重用 */
YFAnnotationView *annotationView = (YFAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
/* 未創(chuàng)建好大頭針視圖時創(chuàng)建 */
if (!annotationView) {
annotationView = [[YFAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
/* image 屬性選用自定義的圖片,創(chuàng)建一次不再創(chuàng)建 */
annotationView.image = [UIImage imageNamed:@"cm2_fm_btn_loved"];
}
/* 當(dāng)模型修改時,更改模型 */
annotationView.annotation = annotation;
return annotationView;
}
@end
- 在 BMKMapViewDelegate 的代理方法中返回自定義的大頭針視圖
#pragma mark *** BMKMapViewDelegate 的代理方法 ***
// -------- 返回自定義的大頭針視圖 --------
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[YFAnnotation class]]) {
return [YFAnnotationView annotationViewWithAnnotation:annotation andMapView:mapView];
}
/* 當(dāng)返回的是 nil 的時候,表示的是默認(rèn)的大頭針視圖 */
return nil;
}
這樣, 我們在進(jìn)行 POI Search 的時候就可以看到自定義的大頭針視圖了, 讓我們來看一下實(shí)現(xiàn)效果
POI Search及自定義大頭針實(shí)現(xiàn)效果