美文网首页iOS开发你需要知道的
iOS 百度SDK - 定位以及poi检索周边

iOS 百度SDK - 定位以及poi检索周边

作者: _白丁_ | 来源:发表于2016-04-15 16:51 被阅读2259次

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 要使用百度地图,请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc] init];
    NSString *baiduKey = @"baidu map app key"; // 您在百度申请的地图App Key
    BOOL iSsuccess = [_mapManager start:baiduKey generalDelegate:self];
    // 如果百度地图引擎启动失败..
    if (iSsuccess == NO) {
        LSLog(@"百度地图引擎启动失败...");
    }else {
        LSLog(@"百度地图引擎启动成功");
    }
}

ViewController

#import <BaiduMapAPI_Search/BMKSearchComponent.h> // 引入检索功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationService.h> // 定位相关的头文件


@interface HomePageViewController ()<BMKLocationServiceDelegate, 
                                     BMKGeoCodeSearchDelegate, 
                                     LocationManagerDelegate>
{
    BMKUserLocation *_oldLocation; ///< 旧的位置
    BMKLocationService* _locService; ///< 百度地图管理类
    BMKGeoCodeSearch *_geoCodeSearch; // 百度逆地理编码
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupNavigation];
    
    // 设置位置管理者的相关配置
     [self setupLocationService];
    
    // 百度反地理编码
    _geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
    
    // 开始定位
    [self startUserLocation];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _locService.delegate = self;
    _geoCodeSearch.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _locService.delegate = nil;
    _geoCodeSearch.delegate = nil;
}
- (void)setupLocationService {
    // 初始化百度位置管理者
    _locService = [[BMKLocationService alloc]init];
}

#pragma mark - Public Methods
/** 开始定位 定位成功后调用 didUpdateBMKUserLocation: */
- (void)startUserLocation {
    [_locService startUserLocationService];
}

/**
 *  用户位置更新后,会调用此函数
 *  @param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    LSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    _oldLocation = userLocation;
    // 经纬度反编码
    [self reverseLocation:userLocation];
}

/** 逆地理编码 */
- (void)reverseLocation:(BMKUserLocation *)userLocation {
    // 定位成功后的 经纬度 -> 逆地理编码 -> 文字地址
    if (_oldLocation) {
        // CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
        CLLocationCoordinate2D pt;
        if (_oldLocation) {
            pt.longitude = _oldLocation.location.coordinate.longitude;
            pt.latitude = _oldLocation.location.coordinate.latitude;
        }
        BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
        reverseGeocodeSearchOption.reverseGeoPoint = pt;
        [_geoCodeSearch reverseGeoCode:reverseGeocodeSearchOption];
    }
}

/**
 *返回地址信息搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结BMKGeoCodeSearch果
 *@param error 错误号,@see BMKSearchErrorCode
 */
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {
    
}

/**
 *返回反地理编码搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结果
 *@param error 错误号,@see BMKSearchErrorCode
 */
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {

    LSLog(@"latitude = %lf longitude = %lf", result.location.latitude, result.location.longitude);
    LSLog(@"%@", result.address);
    if (result.poiList && result.poiList.count > 0) {
        BMKPoiInfo *poiInfo = result.poiList[0];
        _titleLabel.text = [NSString stringWithFormat:@"%@▼", poiInfo.name];
        [self showHint:[NSString stringWithFormat:@"当前位置:%@", poiInfo.name]];
    }
}

相关文章

网友评论

    本文标题:iOS 百度SDK - 定位以及poi检索周边

    本文链接:https://www.haomeiwen.com/subject/brqblttx.html