MKCircle is not updating radius but it's trans

2019-02-07 09:08发布

问题:

I've to draw an MKCicle into an MKMapView. Then I've to re-draw it when user, through a slider, change the radius. I remove it and I re-create it, re-adding it to the map. But instead of do what I'm expecting, I see the MKCircle translating over the map, maintaining the same size.

Here's my code:

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

The NSLog is writing into the console right values, the center doesn't change and the radius changes according to the user input. But, again, the MKCircle translates going on the north-west.

Thanks in advance, Samuel Rabini

回答1:

Fixed. I just add

self.circleView = nil;

before the

[self.mapView addOverlay:self.circle];

in this way it works fine.

Samuel