百度地图使用第六讲:检索及大头针的使用
基本步骤:1、创建地图视图;
2、使用定位的功能获取到当前位置;
3、利用检索功能,在当前位置检索某个关键字(如:酒吧);
4、利用检索到的结果,创建大头针并在地图上展示;
关键的类:
1、BMKMapView:地图
2、BMKLocationService:定位
3、BMKPoiSearch:POI检索(POI即Point Of Interest可以翻译为兴趣点)
4、BMKNearbySearchOption:周边搜索选项,配合POI检索使用,做为POI检索的数据模型
5、BMKPointAnnotation:大头针,可直接添加到地图上做为标注使用
关键方法:
#pragma mark 定位服务的代理方法,如果获取到位置信息则调用- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation#pragma mark POI检索代理方法,如果检索到结果则调此方法-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode
代码示例:#import "SAViewController.h"
#import "BMapKit.h"
@interface SAViewController () <BMKPoiSearchDelegate, BMKMapViewDelegate, BMKLocationServiceDelegate>
@property (nonatomic, strong) BMKMapView *mapView; // 地图
@property (nonatomic, strong) BMKPoiSearch *poiSearch; // POI检索
@property (nonatomic, strong) BMKLocationService *location;// 定位服务
@end
@implementation SAViewController
- (void)viewDidLoad
{
;
// 添加地图视图
self.mapView.frame = self.view.frame;
;
// 开启定位服务
self.location = [ init];
self.location.delegate = self;
;
}
- (void)viewWillAppear:(BOOL)animated
{
;
;
}
- (void)viewWillDisappear:(BOOL)animated
{
;
;
}
- (BMKMapView *)mapView
{
if (!_mapView) {
_mapView = [ init];
_mapView.delegate = self;
}
return _mapView;
}
- (BMKPoiSearch *)poiSearch
{
if (!_poiSearch) {
_poiSearch = [ init];
_poiSearch.delegate = self;
}
return _poiSearch;
}
#pragma mark 定位服务的代理方法,如果获取到位置信息则调用
- (void)didUpdateUserLocation:(BMKUserLocation *)userLocation
{
// 定位成功后将当前的位置设置为地图的中心点
self.mapView.centerCoordinate = userLocation.location.coordinate;
// 地图的缩放比例的17 (3 ~ 19取值)
self.mapView.zoomLevel = 17;
// 停止定位服务
;
// 创建一个搜索选项(检索的数据模型)
BMKNearbySearchOption *nearOption = [ init];
// 检索关键字:“酒吧”
nearOption.keyword = @"酒吧";
// 检索位置为当前定位到的坐标(即我当前的位置)
nearOption.location = userLocation.location.coordinate;
// 开始检索
;
}
#pragma mark POI检索代理方法,如果检索到结果则调此方法
-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
// 检索到相关信息后,对结果进行遍历(结果为BMKPoiInfo类型的数组)
for (BMKPoiInfo *info in poiResult.poiInfoList) {
// 遍历结果中的每一个数据模型并创建一个大头针
BMKPointAnnotation *annotation = [ init];
// 大头针的位置为结果的坐标(经纬度)
annotation.coordinate = info.pt;
// 大头针的标题为结果的名称
annotation.title = info.name;
// 大头针的子标题为结果的地址
annotation.subtitle = info.address;
// 在大头针添加到地图上展示
;
}
}
@end
效果图:
页:
[1]