百度地圖之室內地圖

召喚代碼君:

頭文件

#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.如果定位有偏差,檢查根據地名獲取座標時是否轉換成了百度座標

 */



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章