更新
我需要在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的半径。
发布于 2015-08-18 05:08:09
我解决了!
第1步:
我创建了一个UIView
,并将其作为子视图添加到地图视图中。需要注意的是,我确保将UIView
放在地图视图的中心。这一点很重要,因为您将使用MKMapView
的centerCoordinate
属性来计算半径。
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
下图显示了edgeLocation
和centerLocation
上的注释。
发布于 2019-06-25 08:47:48
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
是为表示半径而创建的自定义圆
https://stackoverflow.com/questions/31997919
复制相似问题