【问题标题】:Scroll MKMapView Underneath Circle Drawn MKMapView在绘制的 MKMapView 的圆圈下方滚动 MKMapView
【发布时间】:2015-08-13 20:34:01
【问题描述】:

更新

我需要在MKMapView 上画一个圆,我可以在其中获得圆的半径及其中心坐标。但是,我还希望圆圈成为MKMapView 的子视图,以便地图视图可以在圆圈下方滚动,在地图移动时更新其中心坐标,并在地图放大和缩小时更新其半径。

有谁知道我如何能够做到这一点?


这是问题的原始措辞

我使用下面的代码在MKMapView 上画了一个圆圈:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    self.region = [MKCircle circleWithCenterCoordinate:self.locationManager.location.coordinate radius:kViewRegionDefaultDistance];
    [self.mapView addOverlay:self.region];
}

- (MKOverlayPathRenderer *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
    MKCircleRenderer *region = [[MKCircleRenderer alloc] initWithOverlay:overlay];
    region.strokeColor = [UIColor blueColor];
    region.fillColor = [[UIColor blueColor] colorWithAlphaComponent:0.4];
    return region;
}

这可以在地图视图上生成一个圆圈。但是,当我滚动地图视图时,圆圈会随之移动。 我希望圆圈保持静止并让地图视图在圆圈下方滚动。

重要的是要注意,我需要获取圆的中心坐标和半径才能创建一个区域。出于这个原因,我不能简单地在 MKMapView 上绘制 UIView,因为我无法获取 UIView 的半径(以米为单位)。

【问题讨论】:

    标签: mkmapview mkoverlay mkoverlaypathrenderer mkcircle


    【解决方案1】:

    我解决了!

    第 1 步:

    我创建了一个UIView 并将其作为子视图添加到地图视图中。需要注意的是,我确保将UIView 置于地图视图的中心。这很重要,因为您将使用MKMapViewcenterCoordinate 属性来计算半径。

    self.region = [[UIView alloc] initWithFrame:centerOfMapViewFrame];
    self.region.contentMode = UIViewContentModeRedraw;
    self.region.userInteractionEnabled = NO;
    self.region.alpha = 0.5;
    self.region.layer.cornerRadius = widthOfView/2;
    self.region.backgroundColor = [UIColor blueColor];
    
    [self.mapView addSubview:self.region];
    

    下图显示了作为子视图添加到mapView 的循环UIView

    第 2 步:

    根据地图视图的中心坐标和 UIView 的边缘坐标计算半径。

    CLLocationCoordinate2D edgeCoordinate = [self.mapView convertPoint:CGPointMake((CGRectGetWidth(self.region.bounds)/2), 0) toCoordinateFromView:self.region]; //self.region is the circular UIView
    
    CLLocation *edgeLocation = [[CLLocation alloc] initWithLatitude:edgeCoordinate.latitude longitude:edgeCoordinate.longitude];
    CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude];
    
    CGFloat radius = [edgeLocation distanceFromLocation:centerLocation]; //is in meters
    

    下图显示了edgeLocationcenterLocation 上的注释。

    【讨论】:

    • 这是一个更稳定的解决方案,它为叠加层添加和删除 MKCircle。感谢您发布它。
    【解决方案2】:

    Swift 4.2/5 适配

    let edgeCoordinate = self.mapView.convert(mapView.center, toCoordinateFrom: overlayView)
    
    let edgeLocation: CLLocation = .init(latitude: edgeCoordinate.latitude, longitude: edgeCoordinate.longitude)
    
    let centerLocation: CLLocation = .init(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
    
    let radius = edgeLocation.distance(from: centerLocation)
    
    // do something with the radius
    

    overlayView 是您创建的用于表示半径的自定义圆

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多