Question

how can I zoom the map on my userLocation automatically in my app? I have the following code to zomm in the map but i must zoom on the userLocation and the following code zooms always to africa?

MKCoordinateRegion zoomIn = mapView.region;
    zoomIn.span.latitudeDelta *= 0.2;
    zoomIn.span.longitudeDelta *= 0.2;
    zoomIn.center.latitude = mapView.userLocation.location.coordinate.latitude;
    zoomIn.center.longitude = mapView.userLocation.location.coordinate.longitude;
    [mapView setRegion:zoomIn animated:YES];
Was it helpful?

Solution

OK i solved the problem, with the following delegate Method:

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

            span.latitudeDelta=0.9;
            span.longitudeDelta=0.9;

            CLLocationCoordinate2D location =mv.userLocation.coordinate;

            location = mv.userLocation.location.coordinate;

            region.span = span;
            region.center = location;
            [mv setRegion:region animated:TRUE];
            [mv regionThatFits:region];
        }
    }
}

OTHER TIPS

thanks alot mate, this helped me greatly. Really wanted to vote but cant because of my rep level.

I also found that using:

mapView.userLocation.location.coordinate.latitude;

always zoomed me to africa, and never centered me onto my current location.

But by using the delegate method:

-(void)mapView:(MKMapView *)myMapView didAddAnnotationViews:(NSArray *)views {

I was able to solve this successfully. What I discovered is that when using mapView.userLocation.location.coordinate.latitude; in the viewDidLoad method wasn't triggering the correct user coords on startup of the view, hence it was displaying the wrong coords.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top