MapView does not reload annotations unless you enlarge or move the map

I use the following for a loop to manage / find the five closest annotations for a user's location, and then set them as mapView annotations.

for (int i = 0; i <= 5; i++) {
            //NSLog(@"Stores Count For Loop: %i", [storesLessThan100KAway count]);
            if ([storesLessThan100KAway count] > 5) {
                NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"distanceToTarget" ascending:YES];
                NSArray *newArray = [storesLessThan100KAway sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
                [mapView addAnnotation:[newArray objectAtIndex:i]];
                if ([[mapView annotations] count] > 4) {
                    [descriptor release];
                    [dict release];
                    [currentlocation release];
                    [usrlocation release];
                    [annotation release];
                    [paul drain];
                    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                    return;
                }
            }

However, annotations added to the map will not be displayed until the map is enlarged or moved. Anyway, could I stop this?

+3
source share
2 answers

MKMapView is a subclass of UIView. Can you try calling [mapView setNeedsDisplay]?

If this does not work:

// Force update of map view.
CLLocationCoordinate2D center = [mapView centerCoordinate];
[mapView setCenterCoordinate:center];
+1
source

Try putting these lines in a timer and stop it after adding annotations

static float deltaX=.1;
CLLocation *location = [[[CLLocation alloc] initWithLatitude:self.mapView.centerCoordinate.latitude longitude:self.mapView.centerCoordinate.longitude] autorelease];

MKCoordinateRegion region;
region.center = location.coordinate;
MKCoordinateSpan span;

span.latitudeDelta = self.mapView.region.span.latitudeDelta+deltaX;
span.longitudeDelta = self.mapView.region.span.longitudeDelta+deltaX;
deltaX+=.1;
deltaX*=-1;
region.span = span; // Set the region span to the new span.
[self.mapView setRegion:region animated:0];
+1

All Articles