AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {// 要使用百度地图,请先启动BaiduMapManager_mapManager = [[BMKMapManageralloc] init];NSString*baiduKey =@"baidu map app key";// 您在百度申请的地图App KeyBOOLiSsuccess = [_mapManager start:baiduKey generalDelegate:self];// 如果百度地图引擎启动失败..if(iSsuccess ==NO) { LSLog(@"百度地图引擎启动失败..."); }else{ LSLog(@"百度地图引擎启动成功"); }}
ViewController
#import// 引入检索功能所有的头文件#import// 定位相关的头文件@interfaceHomePageViewController(){ BMKUserLocation*_oldLocation;///< 旧的位置BMKLocationService* _locService;///< 百度地图管理类BMKGeoCodeSearch*_geoCodeSearch;// 百度逆地理编码}- (void)viewDidLoad { [superviewDidLoad]; [selfsetupNavigation];// 设置位置管理者的相关配置[selfsetupLocationService];// 百度反地理编码_geoCodeSearch = [[BMKGeoCodeSearchalloc]init];// 开始定位[selfstartUserLocation];}- (void)viewWillAppear:(BOOL)animated { [superviewWillAppear:animated]; _locService.delegate=self; _geoCodeSearch.delegate=self;}- (void)viewWillDisappear:(BOOL)animated { [superviewWillDisappear:animated]; _locService.delegate=nil; _geoCodeSearch.delegate=nil;}- (void)setupLocationService {// 初始化百度位置管理者_locService = [[BMKLocationServicealloc]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;// 经纬度反编码[selfreverseLocation: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 = [[BMKReverseGeoCodeOptionalloc]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= [NSStringstringWithFormat:@"%@▼", poiInfo.name]; [selfshowHint:[NSStringstringWithFormat:@"当前位置:%@", poiInfo.name]]; }}