MKCircle is not updating radius but it's translating

一曲冷凌霜 提交于 2020-01-10 16:15: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



来源:https://stackoverflow.com/questions/8765805/mkcircle-is-not-updating-radius-but-its-translating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!