简体   繁体   English

当用户选择位置按钮时,CLLocationManager更新位置

[英]CLLocationManager updates location when user select location button

In my application ,in initial lunch i have to used cllocationmanager class to find current location ([locationManager startUpdatingLocation];) using this method.After find a location i fired this method ([locationManager stopUpdatingLocation];). 在我的应用程序中,在开始吃午饭时,我必须使用cllocationmanager类使用此方法来查找当前位置([locationManager startUpdatingLocation];)。找到位置后,我会触发此方法([locationManager stopUpdatingLocation];)。

Now a user will move from one place to other place.It will automatically detects newlocation or we need to fire ([locationManager startUpdatingLocation];) this method again. 现在,用户将从一个位置移动到另一个位置。它将自动检测到新位置,或者我们需要再次触发([locationManager startUpdatingLocation];)。

Help me? 帮我?

Note: 注意:

If i will call ([locationManager startUpdatingLocation];) method again it will give a newlocation or old location.How to differentiate it ? 如果我再次调用([locationManager startUpdatingLocation];)方法,它将给出一个新位置或旧位置。如何区分它?

Whenever you call [locationManager startUpdatingLocation];, this will give you the current location. 每当您调用[locationManager startUpdatingLocation];时,都会为您提供当前位置。 So, when you call this function again it will give you new location. 因此,当您再次调用此函数时,它将为您提供新的位置。

In such case, either you have to continuously keep on watch for the updated location, if you want automatic update or after certain duration of time you have to check again for updated location and compare it with previous one. 在这种情况下,或者您必须持续监视更新的位置,如果要自动更新,或者在一定时间后必须再次检查更新的位置,并将其与上一个位置进行比较。 By doing this, you will come to know weather is it new location or old. 这样,您将知道天气是新位置还是旧位置。

Short answer: YES, you need to call [locationManager startUpdatingLocation]; 简短的答案:是的,您需要调用[locationManager startUpdatingLocation]; to get a new location. 获取新位置。 You can stop and then start and then stop and then start, and each time you start, you will get the current (new) location of the device. 您可以先停止然后再启动,然后再停止然后再启动,并且每次启动时,您都将获得设备的当前(新)位置。

Long answer: You can also choose to keep the locationManager on (never call stopUpdatingLocation) and get updates all the time. 长答案:您还可以选择保持locationManager处于打开状态(永远不要调用stopUpdatingLocation)并始终获取更新。 By setting the distanceFilter property for the locations manager, you can choose when (or, to be exact, at what distance delta from last update) a new location update will be sent to your app. 通过为位置管理器设置distanceFilter属性,您可以选择将新位置更新发送到您的应用的时间(或更确切地说,与上一次更新之间的距离变化量为多少)。 This seems to be the way used in most implementations that take more than one location sample, or need to track the location of the user constantly. 这似乎是大多数采用不止一个位置样本或需要不断跟踪用户位置的实现方式。

Also note, there is a new startMonitoringSignificantLocationChanges which gives different way to get updates: read the documentation carefully if you want to use this feature, it is not always available. 还要注意,有一个新的startMonitoringSignificantLocationChanges,它提供了不同的获取更新的方式:如果您想使用此功能,请仔细阅读文档,但它并不总是可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM