2015-08-13 87 views
1

UPDATE滚动的MKMapView圆下方绘制的MKMapView

我需要画一个圆到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为单位的半径。

回答

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]; 

图像下面示出了圆形UIView加入作为一个子视图的mapView

enter image description here

步骤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注解。

enter image description here

+0

这为添加和用于覆盖除去MKCircle更稳定的溶液。感谢您发布它。 – Carl