正确使用CLLocationManager的方法(开始/停止更新用户位置) [英] Proper way of using CLLocationManager (to start/stop updating user location)
问题描述
我在UIViewController中有一个带有MKMapView
的UIView
,仅当用户点击按钮时出现.
I have a UIView
with an MKMapView
, in a UIViewController, which appears only when user taps a button.
过程如下:
-
CLLocationManager
对象在header
文件中声明为私有成员. - 显示一个带有
MKMapView
的UIView
(最初,框架在边界之外.根据用户的动作,移动到视图边界之内,在viewController
之内). -
它已初始化:
- A
CLLocationManager
object is declared as private member in theheader
file. - A
UIView
with anMKMapView
is presented (initially the frame is outside bounds. Is moved to within view bounds on user's action, WITHIN THE SAMEviewController
). It is initialized:
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
MKCoordinateSpan span = MKCoordinateSpanMake(0.04, 0.04);
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
mapView.showsUserLocation = YES;
[mapView setRegion:region animated:YES];
附近位置是使用Foursquare API
现在,当view
从可见范围中删除时,我希望停止位置锁定.
我用stopUdatingUserLocation
尝试过.我也released
locationManager
,但是GPS锁定图标在statusBar
中仍然存在.据我了解,连续的GPS锁定会耗尽电池电量,我想停止此操作.我该怎么办?
Now, I wish to stop the location lock, when the view
is removed from visible bounds.
I tried it with stopUdatingUserLocation
. I also released
the locationManager
, but the GPS lock icon is persistent in the statusBar
. As I understand, continuous GPS lock drains the battery, and I would like to stop that. How would I go about it ?
推荐答案
即使尚未正式记录,最好在整个应用程序中仅使用一个CLLocationManager.将其视为单例,不要每次都对其进行初始化,它应该可以正常工作.
Even tho it's not officially documented, it would be better to use only one CLLocationManager throughout your whole app. Treat it as a singleton, don't initialise it every time, and it should work properly.
这篇关于正确使用CLLocationManager的方法(开始/停止更新用户位置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!