MKMapView中showsUserLocation的不稳定行为 [英] Erratic behavior of showsUserLocation in MKMapView
问题描述
我有一个 MKMapView
,带有 MKUserTrackingBarButtonItem
。用户的当前位置应仅显示在关注
或 FollowWithHeading
模式中。实现如下所示:
I have an MKMapView
with an MKUserTrackingBarButtonItem
. The user's current location should only be displayed in the Follow
or FollowWithHeading
modes. The implementation looks like this:
- (void)mapView:(MKMapView *)mapView
didChangeUserTrackingMode:(MKUserTrackingMode)mode
animated:(BOOL)animated
{
[mapView setShowsUserLocation:(mode != MKUserTrackingModeNone)];
}
这是我的代码中唯一一个 setShowsUserLocation的地方
被调用,当按下 MKUserTrackingBarButtonItem
时,它按预期工作。
This is the only place in my code where setShowsUserLocation
is called and it works as expected when the MKUserTrackingBarButtonItem
is pressed.
通过拖动地图更改 UserTrackingMode
时出现问题:在这种情况下,蓝点注释按预期消失,但在大约20%的情况下,当拖动结束时它会重新出现。
My problem occurs when the UserTrackingMode
is changed by dragging the map: in this case, the "blue dot" annotation disappears as intended, but in about 20% of cases it reappears when dragging has ended.
如果我测试地图的 showsUserLocation
属性,它总是设置为NO,即使蓝色点注释是可见的。
If I then test for the map's showsUserLocation
property, it is always set to NO, even though the "blue dot" annotation is visible.
如果您知道如何解决这个问题,我将非常感谢您的帮助。
I'd be very grateful for help if you know how to solve this.
推荐答案
如果用户位置更新在拖动过程中的某个时间进入, didUpdateUserLocation
委托方法仍会触发,即使 showsUserLocation
否
,蓝点也会重新出现。
Looks like if a user location update comes in at a certain time during the drag, the didUpdateUserLocation
delegate method still fires and the blue dot re-appears even though showsUserLocation
is NO
.
这似乎是地图视图的缺点。
This appears to be a shortcoming of the map view.
在 didUpdateUserLocation
中切换 showsUserLocation
似乎修复了地图view的内部状态和蓝点消失:
Toggling showsUserLocation
in didUpdateUserLocation
seems to fix the map view's internal state and the blue dot goes away:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!mapView.showsUserLocation)
{
mapView.showsUserLocation = YES;
mapView.showsUserLocation = NO;
}
}
这篇关于MKMapView中showsUserLocation的不稳定行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!