高德地图添加大头针和周边搜索

1.集成高德地图:

在高德官网导入sdk,初始化地图

-(void)initMapView

{

    ///地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)

    [AMapServices sharedServices].enableHTTPS = YES;


    ///初始化地图

    _mapView = [[MAMapView alloc]init];

    _mapView.frame = CGRectMake(0, 0, ScreenWidth, _baseView.frame.size.height-45-60);

    _mapView.delegate = self;

    ///把地图添加至view

    [_baseView addSubview:_mapView];

//是否显示路况

        _mapView.showTraffic = YES;

    _mapView.showsLabels = YES;

    _mapView.showsIndoorMapControl = YES;

    [_mapView setZoomLevel:15 animated:YES];

    ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码

    _mapView.showsUserLocation = YES;

    _mapView.userTrackingMode = MAUserTrackingModeFollow;

    _mapView.logoCenter = CGPointMake(-455, _mapView.scaleOrigin.y);

    _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x-55, _mapView.scaleOrigin.y);  //设置比例尺位置

    MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];

    r.showsAccuracyRing = YES;///精度圈是否显示,默认YES

    r.showsHeadingIndicator = YES;///是否显示方向指示(MAUserTrackingModeFollowWithHeading模式开启)。默认为YES

    // r.fillColor = [UIColor redColor];///精度圈 填充颜色, 默认 kAccuracyCircleDefaultColor

    // r.strokeColor = [UIColor blueColor];///精度圈 边线颜色, 默认 kAccuracyCircleDefaultColor

    // r.locationDotBgColor = [UIColor greenColor];///定位点背景色,不设置默认白色

    // r.locationDotFillColor = [UIColor grayColor];///定位点蓝色圆点颜色,不设置默认蓝色

    //r.image = [UIImage imageNamed:@"你的图片"]; ///定位图标, 与蓝色原点互斥

    _mapView.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

    [self addLongPressGestureRecognizer];

    [_mapView updateUserLocationRepresentation:r];

   [self removeLogo];

}

//去掉高德的logo

-(void)removeLogo

{

    [self.mapView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if ([obj isKindOfClass:[UIImageView class]]) {

            UIImageView* logoM = obj;

            logoM.layer.contents = (__bridge id)[UIImage imageNamed:@""].CGImage;

        }

    }];

}

//注意设置地图缩放级别,如果不需要显示定位小蓝点但地图初始显示位置不对(可能表现为很多小格子),需要设置地图中心位置或显示区域

//长按地图某一点,添加大头针

-(void)addLongPressGestureRecognizer

{

    UILongPressGestureRecognizer *addPin = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(addPin:)];

    [_mapView addGestureRecognizer:addPin];

}

- (void)addPin:(UILongPressGestureRecognizer *)sender

{

    if (sender.state != UIGestureRecognizerStateBegan) {

        return;

    }


    CGPoint point = [sender locationInView:_mapView];

    //    通过地图类的对象  把点转换成经纬度

    CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:sender.view];

    [self setLocationWithCoordinate:coordinate];

}

- (void)setLocationWithCoordinate:(CLLocationCoordinate2D)coordinate{

   NSString *latitudeStr = [NSString stringWithFormat:@"%f",coordinate.latitude];

    NSString *longitudeStr = [NSString stringWithFormat:@"%f",coordinate.longitude];

    NSString* mapCoordinate = [NSStringstringWithFormat:@"%@,%@",latitudeStr,longitudeStr];

    //NSLog(@"%@",_mapCoordinate);

    //反编码 经纬度---->位置信息

    CLLocation *location=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];

    CLGeocoder *geocoder=[[CLGeocoder alloc]init];

    [geocoderreverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        NSString*street;

        NSString*name;

        if(error) {

            NSLog(@"反编码失败:%@",error);

            [self.view makeToast:@"该网点经纬度信息有误,请重新标注!"];

            street  =@"该网点经纬度信息有误,请重新标注";

            name    =  @"未知地点";

        }else{

            //NSLog(@"反编码成功:%@",placemarks);

            CLPlacemark*placemark=[placemarkslastObject];

            //NSLog(@"%@",placemark.addressDictionary[@"FormattedAddressLines"]);

            NSDictionary*addressDic=placemark.addressDictionary;

            street=[addressDicobjectForKey:@"Street"];

            name=[addressDicobjectForKey:@"Name"];

        }


        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];

        pointAnnotation.coordinate= coordinate;

        pointAnnotation.title= name;

        pointAnnotation.subtitle= street;

        [_mapViewaddAnnotation:pointAnnotation];


//设置该annotation选中状态

        [_mapView selectAnnotation:pointAnnotation animated:YES];

    }];

}

//当移动地图后设置地图回到当前用户位置

-(void)backToLocation

{

    self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;

}

#pragma mark 定位更新回调

-(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation

updatingLocation:(BOOL)updatingLocation

{

    if(updatingLocation)

    {

        //取出当前位置的坐标

        //        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);

    }

    _currentLocation = [userLocation.location copy];

    [self reGeoCoding];

    if (_firstLocate) {

//当第一次进入该页面,就搜索周边数据

        [self SearchPOI];

    }

}

-(void)SearchPOI{

    //周边搜索

    _POISearchManager = [[AMapSearchAPI alloc] init];

    _POISearchManager.delegate = self;

    _POISearchManager.timeout = 3;

    [self startSearchHandle];

}

-(void)startSearchHandle

{

    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];

    request.location            = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];

    request.keywords            =_keyworkds;//可以是@"地铁"等

    /* 按照距离排序. */

    request.sortrule            =0;

    request.requireExtension    =YES;

    request.radius=10000;

    [self.POISearchManager AMapPOIAroundSearch:request];

}

//周边搜索回调

- (void)onPOISearchDone:(AMapPOISearchBaseRequest*)request response:(AMapPOISearchResponse*)response

{

    [HUD hide];

    self.dataArray = [response.pois mutableCopy];

    if(response.pois.count==0)

    {

        NSString *alert = [NSString stringWithFormat:@"周边暂无%@信息",_keyworkds];

        [self.view showCenterToastWithMessage:alert];

        _addressLabel.text = @"";

        _routeLabel.text = @"";

        [self.tableView reloadData];

        return;

    }

    dispatch_async(dispatch_get_main_queue(), ^{

        AMapPOI *poi = self.dataArray[0];

        _addressLabel.text = poi.name;

        _routeLabel.text = poi.address;

        _clickLocation = [[CLLocation alloc]initWithLatitude:poi.location.latitude longitude:poi.location.longitude];;

        [self createAnnotationView];

        _firstLocate = NO;

        [self.tableView reloadData];

    });

}

//添加大头针

-(void)createAnnotationView

{

    [_mapView removeAnnotations:_mapView.annotations];

    NSMutableArray *array = [[NSMutableArray alloc]init];

    for(inti =0; i

        AMapPOI*poi =self.dataArray[i];

        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];

        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);

        pointAnnotation.coordinate= coordinate;

        pointAnnotation.title= poi.name;

        pointAnnotation.subtitle= poi.address;

        [arrayaddObject:pointAnnotation];

       // [_mapView addAnnotation:pointAnnotation];

    }

    [_mapView addAnnotations:array];

}

- (MAAnnotationView*)mapView:(MAMapView*)mapView viewForAnnotation:(id)annotation

{


    if ([annotation isKindOfClass:[MAPointAnnotation class]])

    {

        if ([annotation isKindOfClass:[MAUserLocation class]]) {

            returnnil;

        }

        static NSString *reuseIndetifier = @"annotationReuseIndetifier";

        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

        if(annotationView ==nil)

        {

            annotationView = [[MAAnnotationViewalloc]initWithAnnotation:annotation

                                                          reuseIdentifier:reuseIndetifier];

        }

        UIImage*image;

        if(annotation.coordinate.latitude == _clickLocation.coordinate.latitude){

            image = [UIImage imageNamed:@"clickAnnotation"];

        }else  {

            image = [UIImage imageNamed:@"annotationView"];

        }

        annotationView.image= image;

        //设置中心点偏移,使得标注底部中间点成为经纬度对应点

        annotationView.centerOffset=CGPointMake(0, -18);

        returnannotationView;

    }

    return nil;

}

//点击大头针的方法

- (void)mapView:(MAMapView*)mapView didSelectAnnotationView:(MAAnnotationView*)view

{

    _clickLocation = [[CLLocation alloc]initWithLatitude:view.annotation.coordinate.latitude longitude:view.annotation.coordinate.longitude];

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

    for(inti=0; i

    {

        if (view.annotation.coordinate.latitude == ((id)array[i]).coordinate.latitude)

        {

            //获取到当前的大头针 你可以执行一些操作

            if ([array[i] isKindOfClass:[MAPointAnnotation class]]) {

                MAPointAnnotation*annotation = array[i];

                _addressLabel.text= annotation.title;

                _routeLabel.text= annotation.subtitle;

            }

        }

    }

//添加大头针前需要先remove一次

    [_mapView removeAnnotations:_mapView.annotations];

    [_mapView addAnnotations:array];

}

你可能感兴趣的:(高德地图添加大头针和周边搜索)