CLLocationManager not stopping/releasing

与世无争的帅哥 提交于 2019-12-12 03:04:12

问题


I'm using MKMapView and I start the map at the last known location with CLLocationManager, the problem is that the iPhone and Location Services reports that I'm still using the services after I'm done which rises some concerns about battery usage.

So, please help me release this properly.

CLLocationManager * MANG = [[CLLocationManager alloc] init];
[MANG startMonitoringSignificantLocationChanges];
if(MANG.location){
   [mapa setCenterCoordinate:MANG.location.coordinate animated:NO];
}
[MANG stopMonitoringSignificantLocationChanges];
[MANG stopUpdatingLocation];
[MANG release];

回答1:


There are two ways to fetch the location using Location Services: the first one, less accurate but more battery friendly; and the second one more accurate.

When you declare [myLocationManager startMonitoringSignificantLocationChanges] your iPhone checks for location every time you left a cell tower and enter into a new one. When you declare [myLocationManager startUpdatingLocation] your iPhone checks for location every time the GPS detects a position change (maybe each 1-3 meters, depending on accuracy).

So the thing is, in your code you’re trying to stop the declared Location Manager twice. Just remove the second stop instruction and you’ll get the correct code:

CLLocationManager * MANG = [[CLLocationManager alloc] init];
[MANG startMonitoringSignificantLocationChanges];
if(MANG.location){
   [mapa setCenterCoordinate:MANG.location.coordinate animated:NO];
}
[MANG stopMonitoringSignificantLocationChanges];
[MANG release];


来源:https://stackoverflow.com/questions/7881743/cllocationmanager-not-stopping-releasing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!