高德任意范围搜索

我很懒,对我很懒。
首先获取高德的SDK,注册高德开发者在此就不多说了。通过cocopods或者手动导入SDK,
在这里我是使用cocopods,对我很懒。
在AppDelegate.h或.m文件引入<AMapFoundationKit/AMapFoundationKit.h>,然后在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[AMapServices sharedServices].apiKey = (NSString *)APIKey;
return YES;
}

没什么问题,注册应该成功。
然后在某个ViewController(这里就叫MapVC好了)中引入
<AMapFoundationKit/AMapFoundationKit.h>
<MAMapKit/MAMapKit.h>
<AMapSearchKit/AMapSearchKit.h>
定义以下相关属性

@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapSearchAPI *search;
@property (nonatomic , strong) AMapPOIPolygonSearchRequest *request;
@property (nonatomic , strong) DrawView * drawView;
@property (nonatomic , strong) UISearchBar * keyWordSearchBar;

然后你会发现又一个DrawView的类,对,这是要自己写的一个画图用的view。实现方法如下:
DrawView.h

- (void)getPointFromView:(void(^)(CGPoint drawPoint, BOOL finish,BOOL start))drawPoint;

DrawView.m

@interface DrawView()
@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@property (nonatomic,strong) UIBezierPath *beizer;
@property (nonatomic,assign) CGPoint startPoint;
@property (nonatomic,assign) CGPoint movePoint;
@property (nonatomic , copy) void(^getPointBlock)(CGPoint,BOOL,BOOL);
@end

@implementation DrawView
//重写方法initWithFrame,然后实现一个拖拽手势,这里不是滑动啊
- (instancetype)initWithFrame:(CGRect)frame
{
if ([super initWithFrame:frame]) {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTouch:)];
[self addGestureRecognizer:pan];
self.beizer = [UIBezierPath bezierPath];
[self initCAShaper];
}
return self;
}
-(void)initCAShaper{
self.shapeLayer = [[CAShapeLayer alloc] init];
self.shapeLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
self.shapeLayer.fillColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.7 alpha:0.2].CGColor;
self.shapeLayer.lineCap = kCALineCapRound;
self.shapeLayer.strokeColor = [UIColor cyanColor].CGColor;
self.shapeLayer.lineWidth = 2;
[self.layer addSublayer:self.shapeLayer];
}
-(void)panTouch:(UIPanGestureRecognizer *)sender{
_startPoint = [sender locationInView:self];
if (sender.state == UIGestureRecognizerStateBegan) {
self.shapeLayer.hidden = NO;
[self.beizer moveToPoint:_startPoint];
self.getPointBlock(self.startPoint,NO,YES);
}
if (sender.state == UIGestureRecognizerStateChanged) {
_movePoint = [sender locationInView:self];
[_beizer addLineToPoint:_movePoint];
self.shapeLayer.path = _beizer.CGPath;
self.getPointBlock(self.startPoint,NO,NO);
}
if (sender.state == UIGestureRecognizerStateEnded) {
[_beizer closePath];
[_beizer removeAllPoints];
self.shapeLayer.hidden = YES;
self.getPointBlock(self.startPoint,YES,NO);
}
}
- (void)getPointFromView:(void (^)(CGPoint, BOOL, BOOL))drawPoint
{
self.getPointBlock = drawPoint;
}
@end

之后我们返回地图MapVC,并初始化地图和协议。<MAMapViewDelegate, AMapSearchDelegate,UISearchBarDelegate>

self.mapView = [[MAMapView alloc] initWithFrame:self.view.frame];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
self.search = [[AMapSearchAPI alloc] init];
self.search.delegate = self;
self.request = [[AMapPOIPolygonSearchRequest alloc] init];

然后创建一些简单的按钮,作为操作,请不要太在意这里的界面丑还是美

UIButton * draBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
[draBtn setTitle:@"画图搜索" forState:UIControlStateNormal];
[draBtn setTitle:@"取消" forState:UIControlStateSelected];
[draBtn addTarget:self action:@selector(drawSreach:) forControlEvents:UIControlEventTouchUpInside];
draBtn.tag = 101;
[draBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[draBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
draBtn.selected = NO;
UIBarButtonItem * addDrawItem = [[UIBarButtonItem alloc] initWithCustomView:draBtn];
self.navigationItem.rightBarButtonItems = @[addDrawItem];
UIButton *  clearnBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, self.view.frame.size.height - 100, 40, 40)];
[clearnBtn addTarget:self action:@selector(clearnOverlay:) forControlEvents:UIControlEventTouchUpInside];
clearnBtn.backgroundColor = [UIColor colorWithRed:0.1 green:0.7 blue:0.1 alpha:0.8];
[clearnBtn setTitle:@"清除" forState:UIControlStateNormal];
[self.view addSubview:clearnBtn];

然后实现自定义几个方法

[self drawingAtScreen];
[self OpenLocation];
[self createSearchBar];

以下是方法的实现

- (void)OpenLocation
{
// 开启定位
self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MAUserTrackingModeFollow;
}
//隐藏和显示画图
- (void)drawSreach:(UIButton *)drawBtn
{
drawBtn.selected = !drawBtn.selected;
if (drawBtn.selected) {
self.drawView.hidden = NO;
}
else
{
self.drawView.hidden = YES;
}
}
#pragma mark ---实现屏幕上实时绘图
-(void)drawingAtScreen{
self.drawView = [[DrawView alloc] initWithFrame:self.mapView.frame];
self.drawView.hidden = YES;
[self.view addSubview:self.drawView];
Weak_selft(weakSelf);
__block NSMutableArray * points = [NSMutableArray new];
//获取在屏幕上的坐标,Viewpoint
[self.drawView getPointFromView:^(CGPoint drawPoint, BOOL finish, BOOL start) {
if (start) {
//第二次开始的时候清除上一次画图记录
[points removeAllObjects];
[weakSelf.mapView removeOverlays:weakSelf.mapView.overlays];
[weakSelf.mapView removeAnnotations:self.mapView.annotations];
}
//把屏幕坐标转化为CLLocationCoordinate2D经纬度,百度的貌似也是用这个,ARCGis地图就不是,有自己的方法
CLLocationCoordinate2D cl2d = [weakSelf.mapView convertPoint:drawPoint toCoordinateFromView:weakSelf.mapView];
[points addObject:[AMapGeoPoint locationWithLatitude:cl2d.latitude longitude:cl2d.longitude]];
if (finish) {
[weakSelf PlacePolygonSearch:points];
}
}];
}
//移除地图上的画图和搜索记录
- (void)clearnOverlay:(UIButton *)btn
{
[self.mapView removeOverlays:self.mapView.overlays];
[self.mapView removeAnnotations:self.mapView.annotations];
}
//建立搜索条件
- (void)PlacePolygonSearch:(NSArray * )points{    
[self.request setPolygon:[AMapGeoPolygon polygonWithPoints:points]];
//    self.request.keywords = @"电影院";   
 self.request.requireExtension = YES;  
  [self.request setOffset:50];       
 [self.search AMapPOIPolygonSearch:self.request];    
[self addMAPolygonWithPoints:points];
}
- (void)addMAPolygonWithPoints:(NSArray*)points
{
unsigned long count = [points count];
CLLocationCoordinate2D coordinates[count];
for (int i = 0; i < count; i++)
{
coordinates[i] = CLLocationCoordinate2DMake([[points objectAtIndex:i] latitude], [[points objectAtIndex:i] longitude]);
}
MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coordinates count:count];
[self.mapView addOverlay:polygon];
}
- (void)addAnnotationsWithPOIs:(NSArray *)pois
{
[self.mapView removeAnnotations:self.mapView.annotations];
for (AMapCloudPOI *aPOI in pois)
{
CloudPOIAnnotation *ann = [[CloudPOIAnnotation alloc] initWithCloudPOI:aPOI];
[self.mapView addAnnotation:ann];
}
[self.mapView showAnnotations:self.mapView.annotations animated:YES];
}
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"搜索出错%@",error);
}
}
#pragma mark - AMapSearchDelegate
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{  
  [self addAnnotationsWithPOIs:[response pois]];
}
#pragma mark - MAMapViewDelegate
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation{   
 if ([annotation isKindOfClass:[CloudPOIAnnotation class]])    {       
 static NSString *pointReuseIndetifier = @"PlacePolygonSearchIndetifier";        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];      
  if (annotationView == nil)        {          
  annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];       
 }            
     annotationView.canShowCallout  = YES;  
      annotationView.animatesDrop    = NO;   
     annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                return annotationView;   
 } 
       return nil;
}
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id)overlay
{
if ([overlay isKindOfClass:[MAPolygon class]])
{
MAPolygonRenderer *polygonRenderer = [[MAPolygonRenderer alloc] initWithPolygon:(MAPolygon *)overlay];
polygonRenderer.lineWidth      = 1.f;
polygonRenderer.lineDash = YES;
polygonRenderer.strokeColor    = [UIColor blueColor];
polygonRenderer.fillColor      = [UIColor colorWithRed:1 green:0 blue:0 alpha:.3];
return polygonRenderer;
}
return nil;
}
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
}
- (void)createSearchBar
{
self.keyWordSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10,5, self.view.frame.size.width * 2/3, 30)];
self.keyWordSearchBar.delegate = self;
self.keyWordSearchBar.returnKeyType = UIReturnKeyDone;
[self.navigationController.navigationBar addSubview:self.keyWordSearchBar];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.request.keywords = searchText;
}
//默认的周边搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.keyWordSearchBar resignFirstResponder];
//    UIButton * btn = (UIButton *)[[UIApplication sharedApplication].keyWindow viewWithTag:101];
//    btn.selected = NO;
//    [self drawSreach:btn];
AMapPOIAroundSearchRequest * request = [[AMapPOIAroundSearchRequest alloc] init];
request.keywords = searchBar.text;
request.location = [AMapGeoPoint locationWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude];
request.radius = 3000;
[self.search AMapPOIAroundSearch:request];
}
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
[self.keyWordSearchBar resignFirstResponder];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,992评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,212评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,535评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,197评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,310评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,383评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,409评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,191评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,621评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,910评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,084评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,763评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,403评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,083评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,318评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,946评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,967评论 2 351

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,932评论 25 707
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,644评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,079评论 4 62
  • 我们在之前的一篇文章里面给大家介绍了“正确训练兔子上厕所的简单三步法”,我想兔子家长们一定都掌握了吧。兔子学会了上...
    好睐鼠阅读 3,128评论 0 0
  • 2017年9月3日 星期日 今天晚上看《80天环游地球》第五章,奇特的新股票。 斐利亚·福克要用80天环...
    鑫隆妈妈阅读 160评论 0 0