首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在绘制的圆形MKMapView下滚动MKMapView

在绘制的圆形MKMapView下滚动MKMapView
EN

Stack Overflow用户
提问于 2015-08-14 04:34:02
回答 2查看 261关注 0票数 1

更新

我需要在MKMapView上画一个圆,在那里我可以得到圆的半径和它的中心坐标。但是,我也希望圆圈是MKMapView的子视图,这样地图视图就可以在圆圈下面滚动,随着地图的移动更新它的中心坐标,并在地图放大和缩小时更新它的半径。

有人知道我是如何做到这一点的吗?

这是问题的原文。

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

代码语言:javascript
复制
- (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的半径。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-18 05:08:09

我解决了!

第1步:

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

代码语言:javascript
复制
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的边缘坐标计算半径。

代码语言:javascript
复制
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上的注释。

票数 1
EN

Stack Overflow用户

发布于 2019-06-25 08:47:48

Swift 4.2/5适应

代码语言:javascript
复制
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是为表示半径而创建的自定义圆

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31997919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档