正确使用CLLocationManager的方法(开始/停止更新用户位置) [英] Proper way of using CLLocationManager (to start/stop updating user location)

查看:982
本文介绍了正确使用CLLocationManager的方法(开始/停止更新用户位置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIViewController中有一个带有MKMapViewUIView,仅当用户点击按钮时出现.

I have a UIView with an MKMapView, in a UIViewController, which appears only when user taps a button.

过程如下:

  • CLLocationManager对象在header文件中声明为私有成员.
  • 显示一个带有MKMapViewUIView(最初,框架在边界之外.根据用户的动作,移动到视图边界之内,在viewController之内).
  • 它已初始化:

  • A CLLocationManager object is declared as private member in the header file.
  • A UIView with an MKMapView is presented (initially the frame is outside bounds. Is moved to within view bounds on user's action, WITHIN THE SAME viewController).
  • 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屋!

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