Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone zoom to user location on mapkit

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?

like image 573
Satyam Avatar asked Jun 16 '10 17:06

Satyam


3 Answers

mapView.userLocation is set to a location off the coast of Africa (0,0) 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];
        }
    }
}
like image 183
Trevor Avatar answered Oct 09 '22 00:10

Trevor


you can try:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;

UserTrackingMode will zoom to user Location.

like image 23
Jonathan Gurebo Avatar answered Oct 09 '22 01:10

Jonathan Gurebo


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

like image 1
Claus Broch Avatar answered Oct 08 '22 23:10

Claus Broch