iOS 7-MKCircleRenderer没有被绘制?

时间:2013-11-16 05:22:20

标签: ios objective-c ios7 mkmapview

一直试图解决这个问题几个小时。尝试做一些简单的事情:在mapview上绘制一个圆形叠加层。 BLEvent符合MKMapOverLay协议并具有所需信息(半径,坐标)是我的视图控制器已被设置为地图视图委托。

在我的viewDidLoad ..

 for (BLEvent *event in self.model.events) {
        [self.mapView addOverlay:event];
    }


- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay{

    if ([overlay isKindOfClass:[BLEvent class]]){
        BLEvent *event = (BLEvent *)overlay;

        MKCircle *circle = [MKCircle circleWithCenterCoordinate:event.coordinate radius:event.radius];
        MKCircleRenderer *circleR = [[MKCircleRenderer alloc] initWithCircle:circle];
        circleR.fillColor = [UIColor blueColor];


        return circleR;
    } else{
        return nil;
    }
}

我查了一下,渲染器实际上叫做&gt;我知道坐标是有效的,因为我在地图中显示了注释。

- (void)mapView:(MKMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers{
    NSLog(@"%@", ((MKCircleRenderer *)[renderers objectAtIndex:0]).fillColor);
}

2 个答案:

答案 0 :(得分:3)

未绘制圆圈,因为您创建的MKCircle未添加为地图的叠加层。

为了使这项工作,你的BLEvent类需要是一个MKCircle子类。

然后试试这个:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay{

    if ([overlay isKindOfClass:[BLEvent class]]){
        BLEvent *event = (BLEvent *)overlay;

        MKCircleRenderer *circleR = [[MKCircleRenderer alloc] initWithCircle:event];
        circleR.fillColor = [UIColor blueColor];

        return circleR;
    }
    return nil;
}

答案 1 :(得分:2)

适用于iOS 8.0和SWIFT的工作版

import Foundation
import MapKit

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
    var locationManager: CLLocationManager = CLLocationManager()

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // We use a predefined location
        var location = CLLocation(latitude: 46.7667 as CLLocationDegrees, longitude: 23.58 as CLLocationDegrees)

        addRadiusCircle(location)
    }

    func addRadiusCircle(location: CLLocation){
        self.mapView.delegate = self
        var circle = MKCircle(centerCoordinate: location.coordinate, radius: 10000 as CLLocationDistance)
        self.mapView.addOverlay(circle)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKCircle {
            var circle = MKCircleRenderer(overlay: overlay)
            circle.strokeColor = UIColor.redColor()
            circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
            circle.lineWidth = 1
            return circle
        } else {
            return nil
        }
    }
}