views:

287

answers:

1

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

A: 

Got the first part, made it zoom out again by just calling a function i made that set the region up to be over the US.

-(void)unCenterMap{

MKCoordinateRegion region2 = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region2.center.longitude = (-98.151855); //coordinate for approximately the center of the US
region2.center.latitude = 39.402244;
region2.span.longitudeDelta = 3.0;
region2.span.latitudeDelta = 3.0;


[_mapView setRegion: region2 animated: YES];

}

The second part still baffles me, how do I get that location to pop in again?

Makinitez21