百度地图之室内地图
召喚代碼君:
頭文件
#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中的,在這個(gè)文件中用到了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;//存儲(chǔ)當(dāng)前聚焦的室內(nèi)圖
@property (nonatomic,strong) CLLocation *myLocation;
@end
- (void)viewDidLoad {
? ? [superviewDidLoad];
? ? // Do any additional setup after loading the view.
?? ?
? ? [selfgetCoordinateByAddress:@"浙江省杭州市西湖區(qū)三墩鎮(zhèn)古墩路SCPG印象城購(gòu)物中心"];
?? ?
}
#pragma mark - 根據(jù)地名確定地理坐標(biāo)
-(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(@"位置經(jīng)緯度:%f, %f", location.coordinate.latitude, location.coordinate.longitude);
?? ? ? ?
?? ? ? ?
? ? ? ? //地球坐標(biāo)轉(zhuǎn)百度坐標(biāo),因?yàn)榈乩砭幋a得到的是地球坐標(biāo)(WGS-84),所以要在這里轉(zhuǎn)成百度坐標(biāo)(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; ? //打開(kāi)室內(nèi)地圖
? ? ? ? self.mapView.zoomLevel =19;
?? ? ? ?
? ? ? ? self.poiSearch = [[BMKPoiSearchalloc] init];
? ? ? ? self.routeSearch = [[BMKRouteSearchalloc] init];
?? ? ? ?
? ? ? ? self.mapView.delegate =self;
? ? ? ? self.poiSearch.delegate =self;
? ? ? ? self.routeSearch.delegate =self;
?? ? ? ?
? ? ? ? /*
? ? ? ? //添加室內(nèi)路線檢索入口
? ? ? ? UIBarButtonItem *customRightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"室內(nèi)路線" 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:@"室內(nèi)檢索"forState:UIControlStateNormal];
? ? ? ? [button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
? ? ? ? [button addTarget:selfaction:@selector(handleAction:)forControlEvents:UIControlEventTouchUpInside];
? ? ? ? [self.viewaddSubview:button];
?? ? ? ?
? ? }];
}
#pragma mark - BMKMapView Delegate
//監(jiān)聽(tīng)進(jìn)入和移出室內(nèi)圖時(shí)間
-(void)mapview:(BMKMapView *)mapView baseIndoorMapWithIn:(BOOL)flag baseIndoorMapInfo:(BMKBaseIndoorMapInfo *)info {
?? ?
? ? NSLog(@"flag == %d", flag);
?? ?
? ? if (flag) {
? ? ? ? //進(jìn)入室內(nèi)圖
?? ? ? ?
? ? ? ? NSLog(@"進(jìn)入室內(nèi)圖");
?? ? ? ?
? ? ? ? if (info !=nil && info.arrStrFloors.count >0) {
? ? ? ? ? ? self.indoorMaInfo.strID = info.strID;
? ? ? ? ? ? self.indoorMaInfo.strFloor = info.strFloor;
? ? ? ? ? ? self.indoorMaInfo.arrStrFloors = info.arrStrFloors;
? ? ? ? }
? ? }else {
? ? ? ? //移出室內(nèi)圖
? ? ? ? NSLog(@"移出室內(nèi)圖");
? ? }
?? ?
}
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {
?? ?
? ? NSLog(@"大頭針的設(shè)置方法");
?? ?
? ? if ([annotationisKindOfClass:[RoutAnnotationclass]]) {
? ? ? ? return [(RoutAnnotation *)annotationgetRouteAnnotationView:mapView];
? ? }
? ? returnnil;
}
#pragma mark - handle action
//發(fā)起室內(nèi)檢索
-(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(@"室內(nèi)檢索發(fā)送失敗");
? ? }else {
? ? ? ? NSLog(@"室內(nèi)檢索發(fā)送成功");
? ? }
}
#pragma mark - BMKPoiSearch Delegate
/**
?*?返回POI室內(nèi)搜索結(jié)果
?*
?*? @param searcher? ? ? ? 搜索對(duì)象
?*? @param poiIndoorResult 搜索結(jié)果列表
?*? @param errorCode ? ? ? 錯(cuò)誤號(hào),@see BMKSearchErrorCode
?*/
-(void)onGetPoiIndoorResult:(BMKPoiSearch *)searcher result:(BMKPoiIndoorResult *)poiIndoorResult errorCode:(BMKSearchErrorCode)errorCode {
?? ?
? ? NSLog(@"返回POI室內(nèi)搜索結(jié)果方法");
?? ?
? ? NSArray *array = [NSArrayarrayWithArray:self.mapView.annotations];
? ? [self.mapViewremoveAnnotations:array];
?? ?
? ? if (errorCode ==BMK_SEARCH_NO_ERROR) {
? ? ? ? //成功獲取結(jié)果
? ? ? ? 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(@"檢索失敗,沒(méi)有獲取到結(jié)果");
? ? }
}
//注釋:LocationTransform是在網(wǎng)上找到的坐標(biāo)轉(zhuǎn)化方法,地址:http://www.jianshu.com/p/abdb35b0ba78
/**
?*? 遇到的問(wèn)題:
?*? 1.在handleAction方法中打印 室內(nèi)檢索發(fā)送失敗 時(shí),檢查一下option.indoorId是否存在
? ? 2.如果不走mapview: baseIndoorMapWithIn: baseIndoorMapInfo:方法,檢查一下是否設(shè)置了代理
? ? 3.如果定位有偏差,檢查根據(jù)地名獲取坐標(biāo)時(shí)是否轉(zhuǎn)換成了百度坐標(biāo)
?*/
總結(jié)
- 上一篇: vue中设置html的fontsize,
- 下一篇: th:if 多条件