添加圆形叠加到mapview

时间:2013-04-15 06:41:50

标签: ios ios6 mkmapview cllocation mkoverlay

我正在尝试在ios mapview中添加一个圆形叠加层,在视图中加载我添加了以下代码

CLLocationCoordinate2D center = CLLocationCoordinate2DMake( 53.809638, -1.554586 );
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:center radius:1000];
    circle.title = @"University of Leeds";
    [self.map addOverlay:circle];

还包括委托函数

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleView *circleView = [[MKCircleView alloc] initWithCircle:(MKCircle*)overlay];
        circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
        circleView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
        circleView.lineWidth = 2;
        return circleView;
    }

但它不起作用的原因? 感谢

2 个答案:

答案 0 :(得分:1)

您的map代表是否已设置? 如果您在Interface Builder中创建地图:检查您的地图是否已正确连接。

(请注意,半径以米为单位)

答案 1 :(得分:0)

您必须实现GetOverlayRenderer方法,该方法应创建一个MKCircleRenderer。下面是Xamarin中的一个例子。

MKOverlayRenderer GetOverlayRenderer(MKMapView _nativeMapView, IMKOverlay overlay)
{
    if (overlay is MKCircle)
    {
        // Create a renderer if needed
        if (_circleRenderer == null)
        {
            _circleRenderer = new MKCircleRenderer(overlay as MKCircle);
            _circleRenderer.FillColor = UIColor.Red;
            _circleRenderer.Alpha = 0.3f;
        }
        return _circleRenderer;
    }