2015-08-13 35 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