MKMapView的用户位置在启动或恢复时是错误的 [英] MKMapView's user location is wrong on startup or resume

查看:112
本文介绍了MKMapView的用户位置在启动或恢复时是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我重新启动应用程序或在很长一段时间后恢复运行时,MKMapView的userLocation概念是错误的,并向我展示了它在海中的踪影.

When I start my application fresh, or resume after a long time, MKMapView's notion of the userLocation is wrong and shows me in the middle of the sea.

我正在使用以下代码:

self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
[mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate zoomLevel:ZOOM_LEVEL animated:YES];

在漫长的应用恢复或全新的开始之后发生....

Happens after a lengthy resume of the app or brand new start....

推荐答案

这是预期的行为:iPhone并不总是使用GPS跟踪用户位置(这会消耗大量电池).因此,一旦显示地图, MKMapView 实例就会显示它知道的最后一个最佳"用户位置,然后通过激活跟踪来提高准确性(这是一个无缝过程,您无需不必在意).

That's the expected behavior : the user location isn't always tracked by the iPhone using GPS (it would consume to much battery). So as soon as the map is displayed, the MKMapView instance shows the last 'best' user position it knows and then, improves the accuracy by activating the tracking (this is a seamless process, you don't have to care about it) .

您可以通过实现 MKMapViewDelegate 协议来监视 MKMapView 在地图上更新用户位置的时间.只需实现:

You can monitor when the MKMapView updates the user location on the map by implementing the MKMapViewDelegate protocol. Just implement :

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation      {
    CLLocationAccuracy accuracy = userLocation.location.horizontalAccuracy;
    if (accuracy ......) {
    } 
}

(来自Apple文档的更多信息

(More info from the apple documentation here )

在我的示例中,上面的代码检查mapView当前显示的位置的准确性,并做出相应的反应.

The code above in my example checks the accuracy of the position currently being displayed by the mapView and reacts accordingly.


如果一开始显示用户位置在海中真的很打扰您,则可以隐藏用户位置,直到获得足够准确/新鲜的位置.
为此,首先将 MKMapView showsUserLocation 属性设置为 NO ,直到获得足够准确的位置为止(由于上一个委托回调),然后将其设置为 YES .
这样一来,您就可以避免显示的位置不正确或太旧而无法显示( CLLocation 中有一个 timestamp 属性,以检查它是否为旧位置还是不可以)


If showing the user location in the middle of the sea at first really bother you, you can you hide the user location until you get a location that is accurate/fresh enough.
To do so, set the showsUserLocation property of the MKMapView to NO at first until you get an accurate enough location (thanks to the previous delegate callback) and then set it to YES.
By doing you, you will avoid displaying a location that is not accurate or too old to be diplayed (there is a timestamp property in the CLLocation to check wether it's an old location or not)

NB:您不必不必在您的身边创建一个 CLLocationManager 实例,MKMapView在内部创建一个并发布通过此委托选择器接收的位置.

N.B:You don't have to create a CLLocationManager instance on your side, the MKMapView creates one internally and publish locations it receives via this delegate selector.

这篇关于MKMapView的用户位置在启动或恢复时是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆