简体   繁体   中英

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?

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];
        }
    }
}

you can try:

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

UserTrackingMode will zoom to user Location.

您是否验证过mapView.userLocation返回的位置不是0,0?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM