您的位置:首页 > 移动开发 > IOS开发

iOS开发之百度地图的简单集成——标注&POI检索

2016-06-15 17:40 489 查看

iOS开发之百度地图的简单集成——标注&POI检索

.h文件

//  Created by XK_Recollection on 16/6/15.
//  Copyright © 2016年 GNAMacBook. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>

@interface XK_GasStation_ViewController : UIViewController<BMKMapViewDelegate, BMKLocationServiceDelegate, BMKPoiSearchDelegate, BMKGeoCodeSearchDelegate>

@property (nonatomic, copy) NSString *passName; // 接收上个页面传进来的搜索名字

@property (nonatomic) CLLocationCoordinate2D locationPoint; // 接收地理坐标

@property (nonatomic, copy) NSString *province; // 省份

@property (nonatomic, copy) NSString *city; // 城市


.m文件

//  Created by XK_Recollection on 16/6/15.
//  Copyright © 2016年 GNAMacBook. All rights reserved.
//

#import "XK_GasStation_ViewController.h"
#import "Macros.h"
#import "XK_ShowCell.h"

#define SEARCH_VIEW_HEIGHT kScreenWidth / 7
#define MAP_VIEW_HEIGHT kScreenHeight / 2

@interface XK_GasStation_ViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) BMKMapView* mapView;// 地图

@property (nonatomic, strong) BMKLocationService *locService; // 定位

@property (nonatomic, strong) BMKPoiSearch *poiSearch; // poi搜索

@property (nonatomic, strong) UIView *contentView; // 背景View

@property (nonatomic, strong) UITextField *searchField; // 搜索框

@property (nonatomic, strong) UIButton *searchBtn; // 搜索按钮


// 懒加载
// 地图
- (BMKMapView *)mapView
{
if (!_mapView) {
_mapView = [[BMKMapView alloc] init];
}
return _mapView;
}
// 定位服务
- (BMKLocationService *)locService
{
if (!_locService) {
_locService = [[BMKLocationService alloc] init];
_locService.delegate = self;
}
return _locService;
}


-(void)viewWillAppear:(BOOL)animated
{
[self.mapView viewWillAppear];
self.mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
self.tabBarController.tabBar.hidden = YES;
}

- (void) viewDidAppear:(BOOL)animated
{
// 添加一个PointAnnotation
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = self.locationPoint.latitude;
coor.longitude = self.locationPoint.longitude;

annotation.coordinate = coor;
annotation.title = @"你在这里";
annotation.subtitle = @"没错,就是这里";
[_mapView addAnnotation:annotation];
}

-(void)viewWillDisappear:(BOOL)animated
{
[self.mapView viewWillDisappear];
self.mapView.delegate = nil; // 不用时,置nil
self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad
{
[super viewDidLoad];

// 添加地图View 上面自定义一个View,有textField搜索和一个确定button
self.mapView.frame = CGRectMake(0, SEARCH_VIEW_HEIGHT + 64, kScreenWidth, kScreenHeight - 64 - SEARCH_VIEW_HEIGHT);
[self.view addSubview:self.mapView];

// 标准地图
self.mapView.mapType = BMKMapTypeStandard;
self.mapView.showsUserLocation = YES;
///设定地图View能否支持旋转
self.mapView.rotateEnabled = NO;

_locService = [[BMKLocationService alloc]init];
self.mapView.showsUserLocation = YES;//显示定位图层
_locService.delegate = self;
// 打开定位服务
[_locService startUserLocationService];

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(self.locationPoint.latitude, self.locationPoint.longitude);

/// 当前地图的中心点,改变该值时,地图的比例尺级别不会发生变化
[self.mapView setCenterCoordinate:center];
/// 地图比例尺级别,在手机上当前可使用的级别为3-21级
self.mapView.zoomLevel = 18;

self.poiSearch =[[BMKPoiSearch alloc]init];
self.poiSearch.delegate = self;

// 布局搜索View
[self layoutTopViewAndSubView];
}


#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;
}

//- (void)click
//{
//    NSLog(@"click");
//}

// 长按地图时会调用此方法
- (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate {
//发起检索
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
option.pageIndex = 0;
option.pageCapacity = 20;
option.location = CLLocationCoordinate2DMake(self.locationPoint.latitude, self.locationPoint.longitude);;
option.keyword = self.searchField.text;
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];
}

/**
*返回POI搜索结果
*@param searcher 搜索对象
*@param poiResult 搜索结果列表
*@param errorCode 错误号,@see BMKSearchErrorCode
*/
- (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];
}];

for (BMKPoiInfo *poiInfo in poiResultList.poiInfoList) {
NSLog(@"%@",poiInfo.name);
NSLog(@"%@",poiInfo.address);
}
}
else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
//当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
// result.cityList;
NSLog(@"起始点有歧义");
} else {
NSLog(@"抱歉,未找到结果, %zd", error);
}
}

// 自定义添加大头针方法
- (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];
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
[self.mapView updateLocationData:userLocation];
//  NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}


// 搜索周边 自定义一个button的点击事件
- (void)handleSearch:(UIButton *)sender
{
///周边云检索参数信息类
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
///分页数量,可选,默认为10,最多为50
option.pageCapacity = 10;
///检索的中心点,经纬度
option.location = CLLocationCoordinate2DMake(self.locationPoint.latitude, self.locationPoint.longitude);
///搜索关键字 自定义一个textfield 获取里面的内容
option.keyword = self.searchField.text;
BOOL flag = [_poiSearch poiSearchNearBy:option];
if(flag)
{
NSLog(@"周边检索发送成功");
}
else
{
NSLog(@"周边检索发送失败");
}

// 取消键盘
[UIView animateWithDuration:0.5 animations:^{
[self.searchField resignFirstResponder];
}];
}


#pragma mark 布局View及控件
- (void)layoutTopViewAndSubView
{
// contentView
_contentView = [[UIView alloc] initWithFrame:(CGRectMake(0, 64, kScreenWidth, SEARCH_VIEW_HEIGHT))];
_contentView.backgroundColor = kbgColor;
[self.view addSubview:_contentView];

// 搜索textField
self.searchField = [[UITextField alloc] initWithFrame:(CGRectMake(10, 5, kScreenWidth - 90, CGRectGetHeight(_contentView.frame) - 10))];
self.searchField.placeholder = @"请输入查询内容";
self.searchField.text = self.passName;
self.searchField.delegate = self;
self.searchField.borderStyle = UITextBorderStyleRoundedRect;
[_contentView addSubview:self.searchField];

// 搜索btn
self.searchBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
_searchBtn.frame = CGRectMake(CGRectGetMaxX(_searchField.frame), 5, 80, CGRectGetHeight(_searchField.frame));
_searchBtn.backgroundColor = [UIColor clearColor];
[_searchBtn setTitle:@"搜索" forState:(UIControlStateNormal)];
[_searchBtn setTintColor:[UIColor blackColor]];
[_searchBtn addTarget:self action:@selector(handleSearch:) forControlEvents:(UIControlEventTouchUpInside)];
[self.contentView addSubview:self.searchBtn];
}


参考文档:http://www.cnblogs.com/Jepson1218/archive/2016/03/17/5288287.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: