召唤代码君:
头文件
#import "RoomViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKPoiSearch.h>
#import <BaiduMapAPI_Search/BMKRouteSearch.h>
#import "RoutAnnotation.h"
#import "LocationTransform.h"
//note:RoutAnnotation.h是直接用的百度提供的demo中的,在这个文件中用到了UIImage+Rotate.h,也是用的百度demo中的,百度demo下载地址:http://lbsyun.baidu.com/index.php?title=iossdk/sdkiosdev-download
添加属性:
@interface RoomViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate, BMKPoiSearchDelegate,BMKRouteSearchDelegate>
@property (nonatomic,strong) BMKMapView *mapView; //地图视图
@property (nonatomic,strong) BMKLocationService *service; //定位
@property (nonatomic,strong) BMKPoiSearch *poiSearch; //poi搜索
@property (nonatomic,strong) BMKRouteSearch *routeSearch; //路线搜索
@property (nonatomic,strong) BMKBaseIndoorMapInfo *indoorMaInfo;//存储当前聚焦的室内图
@property (nonatomic,strong) CLLocation *myLocation;
@end
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
[selfgetCoordinateByAddress:@"浙江省杭州市西湖区三墩镇古墩路SCPG印象城购物中心"];
}
#pragma mark - 根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString *)address {
//地理编码
CLGeocoder *geocode = [[CLGeocoderalloc] init];
[geocode geocodeAddressString:addresscompletionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError *_Nullable error) {
CLPlacemark *placemark = [placemarksfirstObject];
CLLocation *location = placemark.location;//位置
NSLog(@"位置经纬度:%f, %f", location.coordinate.latitude, location.coordinate.longitude);
//地球坐标转百度坐标,因为地理编码得到的是地球坐标(WGS-84),所以要在这里转成百度坐标(BD-09)
LocationTransform *locationTranform = [[LocationTransformalloc] initWithLatitude:location.coordinate.latitudeandLongitude:location.coordinate.longitude];
locationTranform = [locationTranform transformFromGDToBD];
CLLocationCoordinate2D afterCoor =CLLocationCoordinate2DMake(locationTranform.latitude, locationTranform.longitude);
//初始化地图
self.mapView = [[BMKMapViewalloc] initWithFrame:self.view.bounds];
self.mapView.centerCoordinate = afterCoor;
self.mapView.baseIndoorMapEnabled =YES; //打开室内地图
self.mapView.zoomLevel =19;
self.poiSearch = [[BMKPoiSearchalloc] init];
self.routeSearch = [[BMKRouteSearchalloc] init];
self.mapView.delegate =self;
self.poiSearch.delegate =self;
self.routeSearch.delegate =self;
/*
//添加室内路线检索入口
UIBarButtonItem *customRightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"室内路线" style:UIBarButtonItemStyleBordered target:self action:@selector(indoorRoutePlanSearch)];
self.navigationItem.rightBarButtonItem = customRightBarButtonItem;
*/
self.indoorMaInfo = [[BMKBaseIndoorMapInfoalloc] init];
[self.viewaddSubview:self.mapView];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(10,200, 80,35);
button.backgroundColor = [UIColorlightGrayColor];
button.layer.cornerRadius =5;
button.layer.masksToBounds =YES;
[button setTitle:@"室内检索"forState:UIControlStateNormal];
[button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(handleAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}];
}
#pragma mark - BMKMapView Delegate
//监听进入和移出室内图时间
-(void)mapview:(BMKMapView *)mapView baseIndoorMapWithIn:(BOOL)flag baseIndoorMapInfo:(BMKBaseIndoorMapInfo *)info {
NSLog(@"flag == %d", flag);
if (flag) {
//进入室内图
NSLog(@"进入室内图");
if (info !=nil && info.arrStrFloors.count >0) {
self.indoorMaInfo.strID = info.strID;
self.indoorMaInfo.strFloor = info.strFloor;
self.indoorMaInfo.arrStrFloors = info.arrStrFloors;
}
}else {
//移出室内图
NSLog(@"移出室内图");
}
}
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
NSLog(@"大头针的设置方法");
if ([annotationisKindOfClass:[RoutAnnotationclass]]) {
return [(RoutAnnotation *)annotationgetRouteAnnotationView:mapView];
}
returnnil;
}
#pragma mark - handle action
//发起室内检索
-(void)handleAction:(UIButton *)button {
NSArray *annotationArr = [NSArrayarrayWithArray:self.mapView.annotations];
[self.mapViewremoveAnnotations:annotationArr];
NSArray *overlayAr = [NSArrayarrayWithArray:self.mapView.overlays];
[self.mapViewremoveOverlays:overlayAr];
BMKPoiIndoorSearchOption *option = [[BMKPoiIndoorSearchOptionalloc] init];
option.indoorId =self.indoorMaInfo.strID;
option.keyword =@"餐厅";
option.pageIndex =0;
option.pageCapacity =20;
BOOL flag = [self.poiSearchpoiIndoorSearch:option];
if (!flag) {
NSLog(@"室内检索发送失败");
}else {
NSLog(@"室内检索发送成功");
}
}
#pragma mark - BMKPoiSearch Delegate
/**
* 返回POI室内搜索结果
*
* @param searcher 搜索对象
* @param poiIndoorResult 搜索结果列表
* @param errorCode 错误号,@see BMKSearchErrorCode
*/
-(void)onGetPoiIndoorResult:(BMKPoiSearch *)searcher result:(BMKPoiIndoorResult *)poiIndoorResult errorCode:(BMKSearchErrorCode)errorCode {
NSLog(@"返回POI室内搜索结果方法");
NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
[self.mapViewremoveAnnotations:array];
if (errorCode ==BMK_SEARCH_NO_ERROR) {
//成功获取结果
for (BMKPoiIndoorInfo *infoin poiIndoorResult.poiIndoorInfoList) {
NSLog(@"name:%@ uid:%@", info.name, info.uid);
BMKPointAnnotation *annotation = [[BMKPointAnnotationalloc] init];
annotation.title = info.name;
annotation.coordinate = info.pt;
[self.mapViewaddAnnotation:annotation];
}
}else {
//检索失败
NSLog(@"检索失败,没有获取到结果");
}
}
//注释:LocationTransform是在网上找到的坐标转化方法,地址:http://www.jianshu.com/p/abdb35b0ba78
/**
* 遇到的问题:
* 1.在handleAction方法中打印 室内检索发送失败 时,检查一下option.indoorId是否存在
2.如果不走mapview: baseIndoorMapWithIn: baseIndoorMapInfo:方法,检查一下是否设置了代理
3.如果定位有偏差,检查根据地名获取坐标时是否转换成了百度坐标
*/