0

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

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

该方法mapView:didUpdateUserLocation:被一遍又一遍地调用。我把 放在MKMapViewDelegate界面上,设置self.mapView.delegate为 self 并设置mapViewWillStartLocatingUser:andmapViewDidStopLocatingUser:只用一个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(@"");
}
4

3 回答 3

0

You are the one who said [self.mapView setShowsUserLocation:YES];. As long as that situation is in force, the map view will continue to track the user's location. If you want to stop tracking, tell the map view to stop tracking.

However, you can change the tracking behavior by using the map view's userTrackingMode (a MKUserTrackingMode value).

于 2015-05-27T16:46:40.153 回答
0

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

[self.locationManager stopUpdatingLocation];

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

self.locationManager.delegate = self;

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

self.locationManager.delegate = nil;
于 2015-05-27T17:09:31.340 回答
0

它的行为完全符合预期。

随着设备的移动,位置更新应该是连续的(但如果它停止,并且精度很低,更新的次数将会减慢或停止)。

但是,从字里行间看,这听起来并不是您的问题,因为您这样说:

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

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

你需要做这些:

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

  • 添加代码以确保resizeRegionToFitAllPins仅在您添加或删除图钉或位置更改一定距离时调用一次。didUpdateLocation如果那是导致您出现问题的原因,则并非每次都会被调用。

于 2015-05-27T17:51:05.537 回答