为什么 MKMapView 的 userLocation 属性有一段时间是垃圾?
我在 IB 中定义了一个地图视图,并将其设置为显示用户位置。
在我的应用程序中,在 -viewDidAppear 中,我查询 self.mapView.userLocation.location.cooperative,它返回的值非常疯狂,例如:
纬度: 4.8194501961644877e-49< br> 经度:2.2993313035571993e-59
但是,下次调用-viewDidAppear时(在我简单地移动到另一个选项卡式视图然后返回到这个视图之后)-viewDidAppear >userLocation 属性保存了我当前位置的正确值。
似乎在我初次调用时,userLocation属性尚未初始化,但尽管已阅读Apple的文档,但我看不到任何警告,其中说该属性仅在执行xxx后才有效。
在 userLocation 有效使用之前是否必须发生一些事情,或者我应该使用 CLLocationManager 并询问它?
预先感谢您的任何帮助。
遗憾的是,托马斯的建议并没有起到什么作用。我后来发现的是:
如果 showsUserLocation 为 NO,则 userLocation 永远不会正确设置,并且 -MapView:didUpdateUserLocation: 永远不会被调用,因此我从来没有得到过合理的位置值。
因此,要获取用户的位置,我必须将 showsUserLocation 设置为 YES,但这意味着在将所有注释添加到视图中(不包括用户的位置)后,我将计算所需的跨度将它们全部包含并以正确的缩放级别显示它们。不过,在我这样做之后,视图会横向跳转,因为地图视图然后会自动将用户的位置显示为蓝色斑点!由于它从未包含在计算缩放级别的注释中,因此我无法将其合并到我的计算中。啊啊!
请注意,当 showsUserLocation 为 YES 时,-MapView:didUpdateUserLocation: 会被调用,但只有在我计算完注释的所有坐标之后,而不是之前!
I have a Map View defined in IB and it is set to show the user location.
In my app, in -viewDidAppear, I query self.mapView.userLocation.location.coordinate and it comes back with insane values such as:
latitude: 4.8194501961644877e-49
longitude: 2.2993313035571993e-59
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.
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.
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:
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.
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!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设它还没有完成查找用户位置 - 它必须解决这个问题,并且可能需要一段时间。
不要在
viewDidLoad
中使用它,而是使用此委托方法:- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation;
您需要设置您的MapView 委托给 self。 :)
I'm assuming it hasn't finished finding the user location - it has to work this out and it may take a while.
Instead of using it in
viewDidLoad
use THIS delegate method:- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation;
You will need to set your mapview delegate to self. :)
核心位置通常也是如此。有时,您会得到在其缓冲区中徘徊的最后一个位置,或者是超宽的在地图上扔飞镖的位置...
最好的办法是检查
.horizontalAccuracy
位置对象的属性并丢弃任何过于模糊的内容。放弃第一个也是一个很好的做法。Same is often true of Core Location. You'll get the last location lingering it its buffer, sometimes, or a super-broad throw-the-dart-at-the-map kind of location...
Best bet is to check the
.horizontalAccuracy
property of the location object and toss any that are too vague. It's good practice to just chuck the first one too.要调用 didUpdateUserLocation 你必须有...
mapView.showsUserLocation = TRUE;
for didUpdateUserLocation to be called you have to have...
mapView.showsUserLocation = TRUE;