【问题标题】:In iOS 7+ MKMapView calls mapView:didUpdateUserLocation: forever在 iOS 7+ MKMapView 调用 mapView:didUpdateUserLocation: 永远
【发布时间】:2015-05-27 16:42:31
【问题描述】:

在我的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: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(@"");
}

【问题讨论】:

    标签: ios objective-c mkmapview mkmapviewdelegate


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你可以使用这个方法来避免方法mapView:didUpdateUserLocation:被一遍又一遍地调用:

      [self.locationManager stopUpdatingLocation];
      

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

      self.locationManager.delegate = self;
      

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

      self.locationManager.delegate = nil;
      

      【讨论】:

        【解决方案3】:

        它的行为完全符合预期。

        位置更新应该随着设备的移动而持续更新(但如果停止,并且准确度低,更新的次数会变慢或停止)。

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

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

        是的,这是可能的,对您来说不可能,这意味着问题的原因不是持续更新,而是您如何设计代码来处理这些持续更新。 不断更新地图是很常见的场景。

        你需要这样做:

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

        • 添加代码以确保仅调用 resizeRegionToFitAllPins 每当您添加或删除图钉或位置更改时 一定的距离。不是每次didUpdateLocation 都被调用 if 这就是给你带来问题的原因。

        【讨论】:

          猜你喜欢
          • 2013-09-24
          • 2013-10-02
          • 2013-12-09
          • 2014-07-20
          • 2020-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-06
          相关资源
          最近更新 更多