views:

395

answers:

2

I'm using map kit and showing user's location using "showsUserLocation" I"m using following code to zoom to user's location, but not zooming. Its zooming to some location in Africa, though the user location on map is showing correct.

MKCoordinateRegion newRegion; 
MKUserLocation* usrLocation = mapView.userLocation; 
newRegion.center.latitude = usrLocation.location.coordinate.latitude; 
newRegion.center.longitude = usrLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 20.0;
newRegion.span.longitudeDelta = 28.0; 
[self.mapView setRegion:newRegion animated:YES];

Why is user's location correctly showing and not zooming properly. Can some one correct me please?

+1  A: 

Have you verified that the location returned by mapView.userLocation isn't at 0, 0?

Claus Broch
If the user location is zero, it wouldn't have shown the annotation pin exactly that matches my location.
Satyam svv
@satyam: with lat/lon delta as you described it, you would be able to include Africa in the zoom since 0,0 is not that far off the coast. Besides, you didn't tell where the user location is.
Claus Broch
+1  A: 

mapView.userLocation is set to a location of the coast when first instantiated. As a result, I do something like the following, which causes the zoom to occur when the annotation appears. Of course, this is just an example:

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
    for(MKAnnotationView *annotationView in views) {
        if(annotationView.annotation == mv.userLocation) {
            MKCoordinateRegion region;
            MKCoordinateSpan span;

            span.latitudeDelta=0.1;
            span.longitudeDelta=0.1; 

            CLLocationCoordinate2D location=mv.userLocation.coordinate;

            region.span=span;
            region.center=location;

            [mv setRegion:region animated:TRUE];
            [mv regionThatFits:region];
        }
    }
}
Trevor