ios 为什么 self.locationManager stopUpdatingLocation 不停止位置更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9469741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why self.locationManager stopUpdatingLocation doesn't stop location update
提问by Canopus
Problem: It seems I can't stop Core Location
from sending updates to my app / tracking.
问题:似乎我无法停止Core Location
向我的应用程序/跟踪发送更新。
What am I doing: In my viewWillAppear
I call self.locationManager
and pass it to a method to show user's location on the map (an instance of MKMapView
). The getter is overridden to check for availability of the serive, to see whether its authorized. If so, it allocs/inits
and startMonitoringSignificantLocationChanges
and returns
.
我在做什么:在我的viewWillAppear
调用中self.locationManager
并将其传递给一个方法以在地图上显示用户的位置(一个实例MKMapView
)。覆盖 getter 以检查服务的可用性,以查看其是否已授权。如果是这样,它allocs/inits
和startMonitoringSignificantLocationChanges
和returns
。
In viewDidDisappear
, I call [self.locationManager stopUpdatingLocation]
. But still I can see the location icon in the status bar. Even when I terminate the app by double tapping the home button and closing it, the icon is still there... even after extended amount of time (10+ minutes). When I go to the Settings App, it tells me that my app is still using location services (purple icon).
在viewDidDisappear
,我打电话[self.locationManager stopUpdatingLocation]
。但是我仍然可以在状态栏中看到位置图标。即使我通过双击主页按钮并关闭它来终止应用程序,图标仍然存在......即使在很长一段时间(10 分钟以上)之后。当我转到设置应用程序时,它告诉我我的应用程序仍在使用位置服务(紫色图标)。
Question:What am I missing here? Why location update doesn't stop?
问题:我在这里缺少什么?为什么位置更新不会停止?
Thanks in advance.
提前致谢。
采纳答案by jnic
The opposite of startMonitoringSignificantLocationChanges
is not stopUpdatingLocation
, it is stopMonitoringSignificantLocationChanges
.
的反面startMonitoringSignificantLocationChanges
不是stopUpdatingLocation
,是stopMonitoringSignificantLocationChanges
。
You probably want to replace startMonitoringSignificantLocationChanges
with startUpdatingLocation
for the sake of more regular updates, unless you have a specific reason for monitoring only for significant location changes.
你可能要替换startMonitoringSignificantLocationChanges
有startUpdatingLocation
更多的定期更新的缘故,除非你有特殊原因只能监控显著位置的变化。
Check out the CLLocation documentationfor further detail.
查看CLLocation 文档以获取更多详细信息。
回答by Christopher
I too just experienced the same problem as Canopus. It appears that even if stopUpdatingLocation is called the navigation pointer still resides on the status bar if I have showUserLocation enabled. Perhaps this is a bug? It may be as I am running and testing with iOS 4.2.1 and this issue may have been fixed in a later SDK.
我也遇到了与 Canopus 相同的问题。似乎即使调用 stopUpdatingLocation 导航指针仍然驻留在状态栏上,如果我启用了 showUserLocation。也许这是一个错误?可能是因为我正在使用 iOS 4.2.1 运行和测试,并且此问题可能已在更高版本的 SDK 中修复。
I would think that if stopUserLocation is called it would also stop showing the user location since the view I am using it in has already disappeared and is subsequently deallocated.
我认为,如果调用 stopUserLocation,它也会停止显示用户位置,因为我在其中使用它的视图已经消失并随后被解除分配。
It appears that you must set showUserLocation to NO before stopping user location updates.
在停止用户位置更新之前,您似乎必须将 showUserLocation 设置为 NO。
Anyway, in my viewDidLoad method I have the following code:
无论如何,在我的 viewDidLoad 方法中,我有以下代码:
self.mapView.showsUserLocation = YES;
More code...
更多代码...
- (void)viewWillDisappear:(BOOL)animated
{
if (locationManager)
{
mapView.showsUserLocation = NO;
[locationManager stopUpdatingLocation];
}
[super viewWillDisappear:animated];
}
- (void)dealloc
{
if (locationManager)
{
[locationManager release];
locationManager = nil;
}
(other code)
}
回答by Juan Boero
Swift:
迅速:
Your map ANDlocation manager both need to be stopped:
您的地图和位置管理器都需要停止:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
locationManager.stopMonitoringSignificantLocationChanges()
locationManager.stopUpdatingLocation()
mapView.showsUserLocation = false
}
You can debug/check location services usage right there in Xcode, in the debug navigator under Energy Impact.
您可以在 Xcode 中的调试导航器中的 Energy Impact 下调试/检查位置服务的使用情况。
回答by iOScar182
I solved this setting nil to locationManager property after delegate
我在委托后解决了这个设置 nil 到 locationManager 属性
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// Your code getting coordinates
//Here set nil
locationManager = nil;
}
回答by John Erck
My app requests "always" auth. Only a certain feature within the app requires that. If the user turns that feature off then on app close we can stop location updates (with the goal of saving battery and removing the location icon from the status bar). I too thought stopping was not working because after app close the location icon in the status bar was still there even though my app was the only app running on my phone with location access and "on app close" I just told it to stop updating locations.
我的应用程序请求“始终”身份验证。只有应用程序中的某个功能需要这样做。如果用户关闭该功能然后在应用程序关闭时我们可以停止位置更新(目的是节省电池并从状态栏中删除位置图标)。我也认为停止不起作用,因为在应用程序关闭后,状态栏中的位置图标仍然存在,即使我的应用程序是我手机上唯一运行的具有位置访问权限的应用程序,并且“在应用程序关闭时”我只是告诉它停止更新位置.
For me the solution was to be a bit more patient and then I realized that it takes iOS about 10-15 seconds to turn off location hardware and update the status bar. I didn't have to nil out my location manager or anything special, just stopped updates on app close, waited 15 seconds, and observed iOS remove the location icon from the status bar. Hope this helps somebody out there!
对我来说,解决方案是多一点耐心,然后我意识到iOS 需要大约 10-15 秒才能关闭定位硬件并更新状态栏。我不必清除我的位置管理器或任何特殊内容,只需在应用程序关闭时停止更新,等待 15 秒,然后观察 iOS 从状态栏中删除位置图标。希望这可以帮助那里的人!
回答by valbu17
I was working with CLLocationManager
in Swift and I think is relevant to the either Swift or Objective-C but, I just created a boolean which I update to true whenever I have received the location update. Since in my case I just need it once on launch.. Example,
我CLLocationManager
在 Swift中工作,我认为与 Swift 或 Objective-C 相关,但是,我刚刚创建了一个布尔值,每当我收到位置更新时,我都会将其更新为 true。因为在我的情况下,我只需要在启动时使用一次..例如,
// my boolean to stop location updates
var alreadyUpdatedLocation = Bool()
Also, in my case I have created a helper function that whenever I get the data/location of the user, I just call my own stopUpdatingLocation
like this,
另外,就我而言,我创建了一个辅助函数,每当我获取用户的数据/位置时,我就像这样调用我自己的函数stopUpdatingLocation
,
// helper function to stop updates
func stopUpdationgLocation() {
// since I received my data within a block, I don't want to just return whenever it wants to :)
dispatch_async(dispatch_get_main_queue()) {
// the building stop updating location function call
self.locationManager.stopUpdatingLocation()
// my own trick to avoid keep getting updates
self.alreadyUpdatedLocation = true
}
}
Then, where ever you use the location updates that you have received, you could do this...
然后,无论您在哪里使用收到的位置更新,都可以执行此操作...
// avoiding multiple calls after I have received user location
if(self.alreadyUpdatedLocation) {
return
}
I hope it helps!
我希望它有帮助!
回答by Kiran K
try this..
尝试这个..
if ([CLLocationManager significantLocationChangeMonitoringAvailable])
{
[self.locationManager startMonitoringSignificantLocationChanges];
}
[[self locationManager] stopUpdatingLocation];
回答by D. Rothschild
If you are using the GoogleMaps SDK for iOS, I found that if you call
如果您使用的是 iOS 版 GoogleMaps SDK,我发现如果您调用
locationManager.stopUpdatingLocation()
and still have
并且仍然有
mapView.isMyLocationEnabled = true
then the gps icon remains.
然后gps图标仍然存在。
What I chose to do is initially show the user's location on the map and then turn it off after 8 seconds. This worked for me using the GoogleMaps SDK. I added this in ViewDidLoad:
我选择做的是最初在地图上显示用户的位置,然后在 8 秒后将其关闭。这对我使用 GoogleMaps SDK 有用。我在 ViewDidLoad 中添加了这个:
DispatchQueue.main.asyncAfter(deadline: .now() + 8.0, execute: {
self.locationManager.stopUpdatingLocation()
self.mapView.isMyLocationEnabled = false
})
You could put the same code in viewWillDisappear if you prefer to not have the gps icon in the status bar when you segue to another view.
如果您希望在转到另一个视图时不在状态栏中显示 gps 图标,则可以在 viewWillDisappear 中放置相同的代码。
回答by Sarthak Sharma
Problem: In my case I use both startMonitoringSignificantLocationChangesand startUpdatingLocation. Even after stopping the location through locationManager.stopMonitoringSignificantLocationChanges()& locationManager.stopUpdatingLocation(). My location is getting called continuously.
问题:就我而言,我同时使用startMonitoringSignificantLocationChanges和startUpdatingLocation。即使在通过locationManager.stopMonitoringSignificantLocationChanges()& locationManager.stopUpdatingLocation()停止位置之后 。我的位置不断被调用。
Solution: 1. Check whether you have invalidated the timers. 2. Initialize locationManager.delegate = nil.
解决方法: 1. 检查是否使定时器失效。2. 初始化locationManager.delegate = nil。
These will surely solve your problem.
这些肯定能解决你的问题。