在iOS 7+中,MKMapView调用mapView:didUpdateUserLocation:forever

时间:2015-05-27 16:42:32

标签: ios objective-c mkmapview mkmapviewdelegate

在我的MapView按钮中点按,我会执行以下操作:

- (IBAction)locate:(id)sender event:(UIEvent*)event {
    DLog(@"");
    if ([self.mapView respondsToSelector:@selector(userTrackingMode)]) {
        [self.mapView setUserTrackingMode:MKUserTrackingModeNone];
    }
    [self.mapView setShowsUserLocation:NO];
    [self.mapView setShowsUserLocation:YES];
}

我看到userLocation pin并调用以下方法来更改地图位置:

- (void) resizeRegionToFitAllPins:(BOOL)includeUserLocation animated:(BOOL)animated {
    if ([self.annotations count] == 1) {
        NSObject<MKAnnotation> *annotation = [self.annotations objectAtIndex:0];
        BOOL isUserLocation = [annotation isKindOfClass:MKUserLocation.class];
        if ((includeUserLocation && isUserLocation) ||
            isUserLocation == NO) {
            CLLocationCoordinate2D coordinate;
            coordinate.latitude = annotation.coordinate.latitude;
            coordinate.longitude = annotation.coordinate.longitude;
            [self setRegion:MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.001f, 0.001f)) animated:animated];
        }
    }
}

我的问题是地图正在永久更新用户的位置(循环中)。 激活用户的位置后无法使用地图。 有什么必要使它停止更新用户的位置并允许用户使用map?

一遍又一遍地调用方法mapView:didUpdateUserLocation:。 我将MKMapViewDelegate放在界面上,将self.mapView.delegate设置为自己,并将mapViewWillStartLocatingUser:mapViewDidStopLocatingUser:设置为仅Dlog(@"")次调用; mapViewDidStopLocatingUser:永远不会被调用。

- (void)mapView:(MKMapView *)map didUpdateUserLocation:(MKUserLocation *)userLocation {
    DLog(@"Resizing...");
    [self.mapView resizeRegionToFitAllPins:YES animated:YES];
    }
}

- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView {
    DLog(@"");
}

- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView {
    DLog(@"");
}

3 个答案:

答案 0 :(得分:0)

你是那个说[self.mapView setShowsUserLocation:YES];的人。只要该情况生效,地图视图将继续跟踪用户的位置。如果您想停止跟踪,请告诉地图视图停止跟踪。

但是,您可以使用地图视图userTrackingModeMKUserTrackingMode值)来更改跟踪行为。

答案 1 :(得分:0)

您可以使用此方法来避免一遍又一遍地调用方法mapView:didUpdateUserLocation:

[self.locationManager stopUpdatingLocation];

在停止更新位置之前,请记住:

self.locationManager.delegate = self;

停止更新位置后,请记住:

self.locationManager.delegate = nil;

答案 2 :(得分:0)

它的行为完全符合预期。

当设备移动时,位置更新应该是连续的(但如果它停止,并且准确性很低,更新的数量将减慢或停止)。

然而,在各行间阅读,听起来确实不是你的问题,因为你这样说:

"It is impossible to use the map after activating the user's location".

是的,这是可能的,事实上你不可能意味着问题的原因不是持续的更新,而是你如何设计代码来处理那些持续的更新。 不断更新地图是一种非常常见的情况。

您需要这样做:

  • 发布一个新问题,显示您的所有代码以及您想要的内容 确定如何正确设计程序以进行处理 持续更新。

  • 添加代码以确保仅调用resizeRegionToFitAllPins 无论何时添加或删除引脚或位置发生变化a 一定的距离。并非每次didUpdateLocation被调用 这就是导致你出现问题的原因。