简体   繁体   English

iphone缩放到mapkit上的用户位置

[英]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. 我正在使用地图工具包并使用“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? 有人可以纠正我吗?

mapView.userLocation is set to a location off the coast of Africa (0,0) when first instantiated. 首次实例化时,mapView.userLocation设置为非洲海岸(0,0)附近的位置。 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. UserTrackingMode将缩放到用户位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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