高德任意范围搜索

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

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

没什么问题,注册应该成功。
然后在某个ViewController(这里就叫MapVC好了)中引入



定义以下相关属性

@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,并初始化地图和协议。

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];
}
高德任意范围搜索_第1张图片

你可能感兴趣的:(高德任意范围搜索)