iphone缩放到mapkit上的用户位置

时间:2022-11-15 20:01:50

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.

我正在使用地图套件并使用“showsUserLocation”显示用户的位置我使用以下代码缩放到用户的位置,但不缩放。它缩放到非洲的某个位置,尽管地图上的用户位置显示正确。

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?

为什么用户的位置正确显示而不是正确缩放。有人可以纠正我吗?

3 个解决方案

#1


26  

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:

首次实例化时,mapView.userLocation设置为非洲海岸(0,0)附近的位置。因此,我执行以下操作,这会导致在注释出现时进行缩放。当然,这只是一个例子:

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

#2


3  

you can try:

你可以试试:

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

UserTrackingMode will zoom to user Location.

UserTrackingMode将缩放到用户位置。

#3


1  

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

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

#1


26  

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:

首次实例化时,mapView.userLocation设置为非洲海岸(0,0)附近的位置。因此,我执行以下操作,这会导致在注释出现时进行缩放。当然,这只是一个例子:

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

#2


3  

you can try:

你可以试试:

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

UserTrackingMode will zoom to user Location.

UserTrackingMode将缩放到用户位置。

#3


1  

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

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