上篇开启百度之旅
3.创建地图周边检索 <BMKMapViewDelegate,BMKLocationServiceDelegate,BMKPoiSearchDelegate>
3.1导入头文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
3.2
{
BMKLocationService* _locService;
BMKPointAnnotation* pointAnnotation;
BMKPoiSearch* _poisearch; int curPage;
}
@property (nonatomic,strong) BMKMapView* mapView;
//判断是不是第一次开启地图
@property (nonatomic,assign) BOOL isfirst;
//结果数组
@property (nonatomic,strong) NSMutableArray *poiListArray;
3.3
/************************检索**************************************/
//这个方法就是在Map移动后执行,所以我们可以在这里获取移动后地图中心点的经纬度了。-(void)mapView:(BMKMapView*)mapView regionDidChangeAnimated:(BOOL)animated
{
BMKCoordinateRegionregion;
CLLocationCoordinate2DcenterCoordinate = mapView.region.center;
region.center= centerCoordinate;
NSLog(@"regionDidChangeAnimated %f,%f ",centerCoordinate.latitude, centerCoordinate.longitude);
//初始化检索对象
_poisearch=[[BMKPoiSearchalloc]init];
_poisearch.delegate=self;
//发起检索
BMKNearbySearchOption*option = [[BMKNearbySearchOptionalloc]init]; option.pageIndex=curPage;
option.pageCapacity=50;
option.location=CLLocationCoordinate2DMake(centerCoordinate.latitude,centerCoordinate.longitude);
option.keyword=@"小区";
BOOLflag = [_poisearchpoiSearchNearBy:option];
if(flag) {
NSLog(@"周边检索发送成功");
} else {
NSLog(@"周边检索发送失败");
}
}
3.4
/** *根据anntation生成对应的View *@param mapView地图View *@param annotation指定的标注 *@return生成的标注View */
- (BMKAnnotationView*)mapView:(BMKMapView*)view viewForAnnotation:(id<BMKAnnotation>)annotation{
//生成重用标示identifier
NSString*AnnotationViewID =@"xidanMark"; //检查是否有重用的缓存
BMKAnnotationView* annotationView = [viewdequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
//缓存没有命中,自己构造一个,一般首次添加annotation代码会运行到此处
if(annotationView ==nil)
{
annotationView = [[BMKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:AnnotationViewID]; ((BMKPinAnnotationView*)annotationView).pinColor=BMKPinAnnotationColorRed;
//设置重天上掉下的效果(annotation)
((BMKPinAnnotationView*)annotationView).animatesDrop=YES;
}
//设置位置
annotationView.centerOffset=CGPointMake(0, -(annotationView.frame.size.height*0.5));
annotationView.annotation= annotation;
//单击弹出泡泡,弹出泡泡前提annotation必须实现title属性
annotationView.canShowCallout=YES;
//设置是否可以拖拽 annotationView.draggable=NO
((BMKPinAnnotationView*)annotationView).animatesDrop=YES;
returnannotationView;
}
3.5
#pragma mark -#pragma mark implement BMKSearchDelegate
/** *返回POI搜索结果 *@param searcher 搜索对象 *@param poiResult 搜索结果列表 *@param errorCode 错误号,@see BMKSearchErrorCode */
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode{
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
//移除原来的大头针
[_mapView removeAnnotations:array];
[self.poiListArray removeAllObjects];
if (errorCode == BMK_SEARCH_NO_ERROR)
{
NSMutableArray *annotations = [NSMutableArray array];
for (int i = 0; i < poiResult.poiInfoList.count; i++)
{
BMKPoiInfo* poi = [poiResult.poiInfoList objectAtIndex:i];
NSLog(@"%@",poi.name);
[self.poiListArray addObject:poi.address];
/**调用大头针*/
pointAnnotation = [[BMKPointAnnotation alloc]init];
pointAnnotation.coordinate =poi.pt;
[annotations addObject:pointAnnotation];
}
/**调用大头针*/
[_mapView addAnnotations:annotations];
[_mapView showAnnotations:annotations animated:YES];
}
}
4.创建地图反检索 <BMKGeoCodeSearchDelegate>
4.1导入头文件
#import<BaiduMapAPI_Search/BMKSearchComponent.h>
4.2
{
BMKGeoCodeSearch* _geocodesearch;
}
4.3
/** *用户位置更新后,会调用此函数 *@param userLocation新的用户位置 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation*)userLocation
{
CLLocationCoordinate2Dpt = (CLLocationCoordinate2D){0,0};//初始化
pt = (CLLocationCoordinate2D){_locService.userLocation.location.coordinate.latitude, _locService.userLocation.location.coordinate.longitude};
BMKReverseGeoCodeOption*reverseGeocodeSearchOption = [[BMKReverseGeoCodeOptionalloc]init];//初始化反编码请求 reverseGeocodeSearchOption.reverseGeoPoint= pt;//设置反编码的店为pt
BOOLflag = [_geocodesearchreverseGeoCode:reverseGeocodeSearchOption];
//发送反编码请求.并返回是否成功
if(flag) {
NSLog(@"反geo检索发送成功");
} else {
NSLog(@"反geo检索发送失败");
}
[_mapViewupdateLocationData:userLocation];
if(_isfirst==YES) {
//那么定位点置中
self.mapView.centerCoordinate=_locService.userLocation.location.coordinate;
}
[_locServicestopUserLocationService];}
4.4
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
[self.poiListArray removeAllObjects];
if (error == 0) {
NSMutableArray *annotations = [NSMutableArray array];
for (int i = 0; i < result.poiList.count; i++) {
BMKPoiInfo* poi = [result.poiList objectAtIndex:i];
NSLog(@"%@",poi.name);
[self.poiListArray addObject:poi.address];
pointAnnotation = [[BMKPointAnnotation alloc]init];
pointAnnotation.coordinate =poi.pt;
[annotations addObject:pointAnnotation];
}
[_mapView addAnnotations:annotations];
[_mapView showAnnotations:annotations animated:YES];
}
}
网友评论