MKCircle没有更新半径,但它正在翻译

时间:2012-01-06 23:26:30

标签: ios mkmapview polygon geometry

我要将MKCicle绘制到MKMapView中。然后,当用户通过滑块更改半径时,我将重新绘制它。我将其删除,然后重新创建它,将其重新添加到地图中。 但是,我没有按照我期望的那样做,而是看到MKCircle在地图上进行翻译,保持相同的大小。

这是我的代码:

- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id)overlay
{
    MKOverlayView* overlayView = nil;

    if(overlay == self.circle)
    {
        //if we have not yet created an overlay view for this overlay, create it now.
        if(nil == self.circleView)
        {
            self.circleView = [[[MKCircleView alloc] initWithCircle:self.circle] autorelease];
            self.circleView.fillColor = [UIColor blueColor];
            self.circleView.strokeColor = [UIColor blueColor];
            self.circleView.alpha = 50;
            self.circleView.lineWidth = 2;
        }

        overlayView = self.circleView;
    }

    return overlayView;
}

-(void)drawPolygonWithLocation
{
    [self.mapView removeOverlay: self.circle];

    MKCoordinateRegion region;
    region.center.latitude = self.geofenceLocation.latitude;
    region.center.longitude = self.geofenceLocation.longitude;
    region.span.latitudeDelta = 0.005;
    region.span.longitudeDelta = 0.005;

    MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits: region];
    [self.mapView setRegion:adjustedRegion animated:TRUE];

    self.radius = (double)(slRadius.value);
    NSLog(@"Raggio: %f", self.radius);
    NSLog(@"Lat: %f, Lon: %f", region.center.latitude, region.center.longitude);
    self.circle = [MKCircle circleWithCenterCoordinate:self.geofenceLocation.coordinate radius: self.radius];
    NSLog(@"CIRCLE: radius %f Lat: %f, Lon: %f", self.circle.radius, self.circle.coordinate.latitude, self.circle.coordinate.longitude);

    [self.mapView addOverlay:self.circle];
}

-(IBAction)updateRadius:(id)sender
{ 
    [self drawPolygonWithLocation];
}

NSLog正在写入控制台右侧值,中心不会改变,半径根据用户输入而变化。 但是,同样,MKCircle翻译在西北方向。

提前致谢, 塞缪尔拉比尼

1 个答案:

答案 0 :(得分:3)

固定。 我只是添加

self.circleView = nil;

之前的

[self.mapView addOverlay:self.circle];

以这种方式工作正常。

塞缪尔