Uncenter MkMapView

Posted by Makinitez21 on Stack Overflow See other posts from Stack Overflow or by Makinitez21
Published on 2010-01-08T12:03:38Z Indexed on 2010/06/05 20:02 UTC
Read the original article Hit count: 371

I am trying to "uncenter" the map view and remove all annotations. the reason for this is that the mapView will be used by different view controllers to display different annotations and locations. What's happening now is that i am using the "remove annotations" method and i removes them, but the map stays centered on the spot and thus when the new annotations come in, it does not move. Is there a way to reset the region and if so, what value do i reset it to. I tried nil, zero and some calculated value but "core animation" says they are all invalid. how do i get that to zoom out and back in on my new annotations?

here is some code

-(void)recenterMap {

NSArray *coordinates = [_mapView valueForKeyPath:@"annotations.coordinate"];
CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};

//NSLog(@"%@", [coordinates description]);

for (NSValue *value in coordinates) {
    CLLocationCoordinate2D coord = {0.0f, 0.0f};
    [value getValue: &coord];

    if(coord.longitude > maxCoord.longitude) {
        maxCoord.longitude = coord.longitude;
    }

    if(coord.latitude > maxCoord.latitude){

        maxCoord.latitude = coord.latitude;
    }

    if(coord.longitude < minCoord.longitude){
        minCoord.longitude = coord.longitude;
    }

    if(coord.latitude < minCoord.latitude){
        minCoord.latitude = coord.latitude;
    }
}


MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;


[_mapView setRegion: region animated: YES];
}

Thats the recenter methos i'm using

- (MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id <MKAnnotation>)annotation{

MKAnnotationView *view = nil;
if(annotation != mapView.userLocation){

    Annotation *schoolAnn = (Annotation*)annotation;
    view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"schoolLoc"];
    if(nil == view){
        view = [[[MKPinAnnotationView alloc] initWithAnnotation:schoolAnn     reuseIdentifier:@"schoolLoc"]autorelease];
    }

    [(MKPinAnnotationView *)view setAnimatesDrop:YES];
    [view setCanShowCallout:YES];

}
else { 
    [self recenterMap];
}

return view;
}

This "else" piece waits until the blue marble drops in then recenters the map. The problem i have with that part is that when i go back to the previous View Controller i remove all annotations, so when i come back in the User location (for some reason) does not pop back in. How do i get this to pop back in?

thanks in advance guys

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c