本文介绍了为什么MKMapView的userLocation属性会垃圾...一段时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IB中定义了一个地图视图,它被设置为显示用户位置。

I have a Map View defined in IB and it is set to show the user location.

在我的应用程序中, -viewDidAppear ,我查询self.mapView.userLocation.location.coordinate,它返回疯狂的值,如:

In my app, in -viewDidAppear, I query self.mapView.userLocation.location.coordinate and it comes back with insane values such as:

纬度:4.8194501961644877e-49

经度:2.2993313035571993e-59

latitude: 4.8194501961644877e-49
longitude: 2.2993313035571993e-59

然而,下次 -viewDidAppear 是调用(在我移动到另一个选项卡视图然后返回到此选项之后) userLocation 属性保存了当前位置的正确值。

However, the next time -viewDidAppear is called (after I've simply moved to another tabbed view and then back to this one) the userLocation property holds exactly the correct values for my current location.

似乎在我初次调用时, userLocation 属性尚未初始化但是尽管已经阅读了Apple的文档,我看不出任何警告说它只是这个属性在执行xxx之后有效。

It seems that at the time of my initial call, the userLocation property has not been initialised but despite having read Apple's documentation I can't see any caveats where it says that this property is only valid after doing xxx.

userLocation 有效使用之前是否必须发生某些事情,或者我应该只使用 CLLocationManager 然后问它呢?

Is there something that has to happen before userLocation is valid to use or should I just use CLLocationManager and ask it instead?

提前感谢您的任何帮助。

Thanks in advance for any help.

可悲的是,托马斯的建议没有救命。我发现的是:

Sadly, Thomas' suggestion didn't help. What I have since discovered is:

如果 showsUserLocation 为NO,则 userLocation 永远不会正确设置 -MapView:didUpdateUserLocation:永远不会被调用,因此我从来没有得到合理的位置值。

If showsUserLocation is NO, then userLocation is never set correctly and -MapView:didUpdateUserLocation: is never called, consequently I never ever get a sensible location value.

因此,要获取用户的位置,我必须设置 showsUserLocation 为YES,但这意味着在我将所有注释添加到视图中后(不包括用户的位置)我然后计算所需的跨度以包含它们并在右侧显示所有缩放级别。在我这样做之后,视图会向侧面跳跃,因为Map View会自动将用户的位置显示为蓝色斑点!因为它从未包含在注释中以计算缩放级别,所以我无法将其合并到我的计算中。 Aaargh!

So, to get the user's location I have to set showsUserLocation to YES, however that then means that after all my annotations have been added to the view (without including the user's location) I then calculate the required span to encompass them all and display them all at the right zoom level. After I do that though, the view jumps sideways as the Map View then automatically displays the user's location as the blue blob! As it was never included in the annotations to work out the zoom level I can't incorporate it into my calculations. Aaargh!

请注意,当 showsUserLocation 为YES时,会调用 -MapView:didUpdateUserLocation:,但只能在我已经计算了我的注释的所有坐标,而不是之前!

Note that when showsUserLocation is YES, then -MapView:didUpdateUserLocation: is called, but only after I've calculated all the coordinates of my annotations, not before!

推荐答案

我假设它还没有找到用户位置 - 它必须解决这个问题,可能需要一段时间。

I'm assuming it hasn't finished finding the user location - it has to work this out and it may take a while.

而不是在 viewDidLoad 中使用它使用THIS委托方法:

Instead of using it in viewDidLoad use THIS delegate method:

- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation;

您需要将mapview委托设置为self。 :)

You will need to set your mapview delegate to self. :)

这篇关于为什么MKMapView的userLocation属性会垃圾...一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:30
查看更多