ios - 授予使用位置服务的权限后 map 未更新

标签 ios mkmapview

我有一个 MKMap。当用户首次登录应用程序时,它会要求使用定位服务。我的问题是,如果用户单击确定, map 不会更新,也不会到达他们的位置。如果用户离开 View 并返回,则 map 有效。我该如何解决这个问题。

我试过了,没有成功-

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusDenied) {

        AMBlurView *mapBlurred = [AMBlurView new];
        mapBlurred.frame = self.view.bounds;
        [self.view addSubview:mapBlurred];

        MKCoordinateSpan span = MKCoordinateSpanMake(52.0f,80.0001f);
        CLLocationCoordinate2D coordinate = {37.7833, 122.4167};
        MKCoordinateRegion region = {coordinate, span};

        MKCoordinateRegion regionThatFits = [self.schoolMap regionThatFits:region];
        NSLog(@"Fit Region %f %f", regionThatFits.center.latitude, regionThatFits.center.longitude);

        [self.schoolMap setRegion:regionThatFits animated:YES];

        [self.view bringSubviewToFront:self.schoolTextBlur];
        [self.view bringSubviewToFront:self.autocompleteTableView];
        [self.view bringSubviewToFront:self.schoolText];
        [self.view bringSubviewToFront:self.theLine];
        [self.view bringSubviewToFront:self.theLine2];

        [self showLocationUnavailable];


    }
    else if (status == kCLAuthorizationStatusAuthorized) {

        MKUserLocation *myLocation = [self.schoolMap userLocation];
        CLLocationCoordinate2D coord = [[myLocation location] coordinate];
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 9000, 9000);
        [self.schoolMap setRegion:region animated:YES];

    }
}

最佳答案

如果您在用户授予获取位置的权限后立即执行此操作,则 [self.schoolMap userLocation] 很可能还没有找到合适的位置。相反,您可以在用户授予权限时将 map 设置为 followUserLocation,因为这将使 map 始终查看用户所在的位置,或者打开 CLLocationManager 将继续触发 didUpdateUserLocation 委托(delegate)方法,直到您获得准确修复,然后告诉它停止。

关于ios - 授予使用位置服务的权限后 map 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652474/

相关文章:

ios - 我试图在模拟器中获取 iPhone 6 屏幕尺寸,但它显示的尺寸错误

ios - 使用 ionic 框架在iphone中获取唯一设备ID

iOS - 按下后退按钮时导航栏颜色转换

ios - NSURLRequest 的默认 setHTTPMethod 是什么?是 "POST"还是 "GET"?

ios - 如何使整个调用可点击?

ios - 我怎样才能使我的 map View 居中,以便选定的图钉不在 map 的中间,而是在 map 的 1/3 处?

iphone - 比较两个数组并获取共同项

iphone - 重绘MKAnnotationView

ios - MKPinAnnotationView 不显示标题

iOS - MKMapView showAnnotations :animated: with padding?