ios 百度地图指定区域_iOS 一步一步实现百度地图范围搜索 · qiuyu’s Blog

实现百度地图显示范围内搜索关键词

效果图:

实现原理: 首先,要实现范围内搜索,就得借助地图搜索类方法,去百度地图SDK里查看,发现根据范围和搜索词的方法正是我们所需要的.

接着去 BMKBoundSearchOption 里查看,发现要实现范围搜索只需要得到左下角和右上角的经纬度坐标点即可。

1.首先加载出百度地图1

2

3

4

5

6

7_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];

// 设置地图级别

[_mapView setZoomLevel:16];

_mapView.delegate= self;

_mapView.isSelectedAnnotationViewFront = YES;

[self.view addSubview:_mapView];

2.在地图加载成功后的方法里去得到左下角和右上角的坐标点的经纬度, 需要一个方法来实现屏幕坐标点转化成经纬度。1

2

3

4

5

6

7

8

9//加载完毕先调取一次检索

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView

{

leftBottomPoint = [_mapView convertPoint:CGPointMake(0,_mapView.frame.size.height) toCoordinateFromView:mapView];  // //西南角(左下角) 屏幕坐标转地理经纬度

rightBottomPoint = [_mapView convertPoint:CGPointMake(_mapView.frame.size.width,0) toCoordinateFromView:mapView];  //东北角(右上角)同上

//开始搜索

[self beginSearch];

}

3.得到俩个点的经纬度就可以开始发起搜索了。1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21- (void)beginSearch{

_poisearch = [[BMKPoiSearch alloc]init];

_poisearch.delegate = self;

BMKBoundSearchOption *boundSearchOption = [[BMKBoundSearchOption alloc]init];

boundSearchOption.pageIndex = 0;

boundSearchOption.pageCapacity = 20;

boundSearchOption.keyword = @"烤鱼";

boundSearchOption.leftBottom =leftBottomPoint;

boundSearchOption.rightTop =rightBottomPoint;

BOOL flag = [_poisearch poiSearchInbounds:boundSearchOption];

if(flag)

{

NSLog(@"范围内检索发送成功");

}

else

{

NSLog(@"范围内检索发送失败");

}

}

4.在搜索结果的代理方法里将搜索到的结果展示出来。1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29#pragma mark implement BMKSearchDelegate

- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error

{

if (error == BMK_SEARCH_NO_ERROR) {

NSArray* array = [NSArray arrayWithArray:_mapView.annotations];

[_mapView removeAnnotations:array];

array = [NSArray arrayWithArray:_mapView.overlays];

[_mapView removeOverlays:array];

//在此处理正常结果

for (int i = 0; i < result.poiInfoList.count; i++)

{

BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];

[self addAnimatedAnnotationWithName:poi.name withAddress:poi.pt];

}

} else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){

NSLog(@"起始点有歧义");

} else {

// 各种情况的判断。。。

}

}

// 添加动画Annotation

- (void)addAnimatedAnnotationWithName:(NSString *)name withAddress:(CLLocationCoordinate2D)coor {

BMKPointAnnotation*animatedAnnotation = [[BMKPointAnnotation alloc]init];

animatedAnnotation.coordinate = coor;

animatedAnnotation.title = name;

[_mapView addAnnotation:animatedAnnotation];

}

5.当地图区域发生改变时,会触发的方法有3个: “正在改变”、”即将改变”、”改变完成”。

很容易就想到,我们需要使用的是”改变完成”的方法,在里面重新请求一次搜索:1

2

3

4

5

6

7

8- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

leftBottomPoint = [_mapView convertPoint:CGPointMake(0,_mapView.frame.size.height) toCoordinateFromView:mapView];  // //西南角(左下角) 屏幕坐标转地理经纬度

rightBottomPoint = [_mapView convertPoint:CGPointMake(_mapView.frame.size.width,0) toCoordinateFromView:mapView];  //东北角(右上角)同上

[self beginSearch];

}

总结: demo只实现了一个很基础的功能,后期还可以增加更加炫酷的功能,比如改变气泡的形状。如果你有更好的想法,欢迎和我交流!

demo地址:https://github.com/wang-qiuyu/BoundsSearchDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值