iOS 高德地图点击事件

本例是以高德地图覆盖物圆作为电子围栏,通过给mapview添加的Tap事件确定圆的经纬度,滑动slider修改圆的半径,来添加电子围栏。

添加代理UIGestureRecognizerDelegate(为了解决点击事件冲突)

- (void)viewDidLoad {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    tap.delegate = self; //一定要记得设置代理

    [self.mapView addGestureRecognizer:tap];

//获取上次设置的圈的经纬度和半径
    self.circle = [MACircle circleWithCenterCoordinate:self.touchMapCoordinate radius:self.Rslider.value];
}
//允许多个交互事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
//点击事件
- (void)tap:(UITapGestureRecognizer *)tap
{
    //移除屏幕上的圆
    [self.mapView removeOverlay:self.circle];
    
    //保存点击获取的经纬度
    self.touchMapCoordinate =
    [self.mapView convertPoint:[tap locationInView:self.mapView] toCoordinateFromView:self.mapView];

    //设置圆的位置
    self.circle.coordinate = self.touchMapCoordinate;
    
    //反地理编码获取详细地址
    [self getAddressByLatitude:self.touchMapCoordinate.latitude longitude:self.touchMapCoordinate.longitude];

    //显示经纬度及半径
    self.latitudeLb.text = [NSString stringWithFormat:@"%f", self.touchMapCoordinate.latitude];
    self.longitudeLb.text = [NSString stringWithFormat:@"%f", self.touchMapCoordinate.longitude];
    self.valueLabel.text = [NSString stringWithFormat:@"%.3f千米", self.Rslider.value/1000];

    //添加新圆到屏幕上
    [self.mapView addOverlay: self.circle];
}
//滑动slider改变圆的半径(ValueChange)
- (IBAction)Rslider:(id)sender {
    self.valueLabel.text = [NSString stringWithFormat:@"%.3f千米", self.Rslider.value/1000];

    //移除屏幕上的圆
    [self.mapView removeOverlay:self.circle];
    self.circle = nil;

    //设置圆的半径(经纬度由点击事件获取)
    self.circle = [MACircle circleWithCenterCoordinate:self.touchMapCoordinate radius:self.Rslider.value];

    //添加新圆到屏幕上
    [self.mapView addOverlay:self.circle];
}

最终效果

iOS 高德地图点击事件_第1张图片

你可能感兴趣的:(iOS 高德地图点击事件)