持续跟踪iOS上的用户位置

时间:2022-11-03 08:58:11

I need to constantly monitor the position of an user to notify him when he gets near something interesting. Which is the correct way to achieve this?

我需要不断地监视用户的位置,以便在他接近有趣的东西时通知他。实现这一目标的正确方法是什么?

I have not been able to keep a timer running in the background (to periodically update user's position on the server), I also read that subscribing to significant location changes could have make me receive location updates even when the app is not running, but i can't get it happen. Am I on the right path? Are there any other options?

我一直无法在后台运行计时器(为了定期更新服务器上的用户位置),我还看到订阅了重要的位置更改,即使应用程序没有运行,也可以让我接收位置更新,但我无法实现。我走对了吗?还有其他选择吗?

EDIT - There is a requisite that i should have exposed and that is probably making me loose the focus on the real problem:

编辑——有一个我应该暴露的必要条件,这可能使我失去了对真正问题的关注:

  • Interesting "stuff" around the user has not a fixed location, so before sending a push I have to be sure that the user is currently in that location (of course i can assume it for n minutes).
  • 用户周围有趣的“东西”没有固定的位置,所以在发送push之前,我必须确保用户当前在那个位置(当然,我可以假设n分钟)。

4 个解决方案

#1


0  

You can set up monitoring location stuff in you VC as below

您可以在您的VC中设置监视位置的内容,如下所示

in viewDidLoad method do as below

在viewDidLoad方法中执行如下操作。

CLLocationManager locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;(Accuracy according to your need)
[locationManager startUpdatingLocation];

than you have to overrite below two optional delegate methods of CLLocationManagerDelegate protocol

比您必须在CLLocationManagerDelegate协议的两个可选委托方法下面重写

for iOS6+

iOS6 +

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}

and for iOS 2 to 6

iOS 2到6也是如此

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

in these methods you will get updated location. use it as you want. every time location updated these method get calls.

在这些方法中,您将获得更新的位置。你想怎么用就怎么用。每次位置更新这些方法时,都会得到调用。

#2


0  

What you need is geofencing. In iOS it's called "region monitoring". You can start with startMonitoringForRegion: in the CLLocationManager guide. More detail is in the Region Monitoring Guide

你需要的是地理信息。在iOS中,它被称为“区域监控”。您可以从startMonitoringForRegion开始:CLLocationManager指南。更详细的情况见区域监测指南

In iOS, regions associated with your app are tracked at all times, including when your app is not running. If a region boundary is crossed while an app is not running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it is woken up and given a short amount of time (around 10 seconds) to handle the event.

在iOS中,与你的应用程序相关的区域在任何时候都被跟踪,包括你的应用程序没有运行的时候。如果在应用程序不运行时跨越了区域边界,则该应用程序将重新启动到后台以处理该事件。类似地,如果事件发生时应用程序被挂起,它将被唤醒,并被给予很短的时间(大约10秒)来处理事件。

#3


0  

As far as I know, significant change location update is the only way to update current location when the app is running in background or not suspended(terminated) by system. when that happens, be sure to check UIApplicationLaunchOptionsLocationKey and initialise CLLocationManager to handle location change notification. If the app is running in foreground, that's simple case.

据我所知,重要的更改位置更新是在后台运行app时更新当前位置的唯一方法。当发生这种情况时,一定要检查UIApplicationLaunchOptionsLocationKey并初始化CLLocationManager来处理位置更改通知。如果应用程序在前台运行,这是很简单的情况。

By the way, in order to let system keep updating location to your app, you must enable maps feature and background modes(location updates) for the project.

顺便说一下,为了让系统不断更新你的应用程序的位置,你必须为项目启用地图功能和背景模式(位置更新)。

read Apple location/Map documents for more details.

更多细节请阅读Apple location/Map文档。

#4


0  

Disclaimer: I work for Cintric

免责声明:我为Cintric公司工作

We had a similar problem at Cintric and actually made a tool just for this purpose. It will track the location in the background even after the app has been quit, using only 1% battery too!

我们在Cintric也有类似的问题,我们为此专门做了一个工具。它会跟踪后台的位置,即使应用已经退出,也只用1%的电池!

You can download the .framework file, drag it into your project and initialize with one line of code: [CintricFind initWithApiKey:@"YOUR_API_KEY_HERE" andSecret:@"YOUR_SECRET_HERE"];

您可以下载.framework文件,将其拖到项目中,并使用一行代码进行初始化:[CintricFind initWithApiKey:@“YOUR_API_KEY_HERE”和secret:@“YOUR_SECRET_HERE”];

You can get an API key for free.

您可以免费获得一个API密钥。

There are docs here: https://www.cintric.com/docs/ios

这里有一些文档:https://www.cintric.com/docs/ios

And there is a blog post explaining further here: http://www.blog.cintric.com/?p=121

还有一篇博文对此作了进一步解释:http://www.blog.cintric.com/?

#1


0  

You can set up monitoring location stuff in you VC as below

您可以在您的VC中设置监视位置的内容,如下所示

in viewDidLoad method do as below

在viewDidLoad方法中执行如下操作。

CLLocationManager locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;(Accuracy according to your need)
[locationManager startUpdatingLocation];

than you have to overrite below two optional delegate methods of CLLocationManagerDelegate protocol

比您必须在CLLocationManagerDelegate协议的两个可选委托方法下面重写

for iOS6+

iOS6 +

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}

and for iOS 2 to 6

iOS 2到6也是如此

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

in these methods you will get updated location. use it as you want. every time location updated these method get calls.

在这些方法中,您将获得更新的位置。你想怎么用就怎么用。每次位置更新这些方法时,都会得到调用。

#2


0  

What you need is geofencing. In iOS it's called "region monitoring". You can start with startMonitoringForRegion: in the CLLocationManager guide. More detail is in the Region Monitoring Guide

你需要的是地理信息。在iOS中,它被称为“区域监控”。您可以从startMonitoringForRegion开始:CLLocationManager指南。更详细的情况见区域监测指南

In iOS, regions associated with your app are tracked at all times, including when your app is not running. If a region boundary is crossed while an app is not running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it is woken up and given a short amount of time (around 10 seconds) to handle the event.

在iOS中,与你的应用程序相关的区域在任何时候都被跟踪,包括你的应用程序没有运行的时候。如果在应用程序不运行时跨越了区域边界,则该应用程序将重新启动到后台以处理该事件。类似地,如果事件发生时应用程序被挂起,它将被唤醒,并被给予很短的时间(大约10秒)来处理事件。

#3


0  

As far as I know, significant change location update is the only way to update current location when the app is running in background or not suspended(terminated) by system. when that happens, be sure to check UIApplicationLaunchOptionsLocationKey and initialise CLLocationManager to handle location change notification. If the app is running in foreground, that's simple case.

据我所知,重要的更改位置更新是在后台运行app时更新当前位置的唯一方法。当发生这种情况时,一定要检查UIApplicationLaunchOptionsLocationKey并初始化CLLocationManager来处理位置更改通知。如果应用程序在前台运行,这是很简单的情况。

By the way, in order to let system keep updating location to your app, you must enable maps feature and background modes(location updates) for the project.

顺便说一下,为了让系统不断更新你的应用程序的位置,你必须为项目启用地图功能和背景模式(位置更新)。

read Apple location/Map documents for more details.

更多细节请阅读Apple location/Map文档。

#4


0  

Disclaimer: I work for Cintric

免责声明:我为Cintric公司工作

We had a similar problem at Cintric and actually made a tool just for this purpose. It will track the location in the background even after the app has been quit, using only 1% battery too!

我们在Cintric也有类似的问题,我们为此专门做了一个工具。它会跟踪后台的位置,即使应用已经退出,也只用1%的电池!

You can download the .framework file, drag it into your project and initialize with one line of code: [CintricFind initWithApiKey:@"YOUR_API_KEY_HERE" andSecret:@"YOUR_SECRET_HERE"];

您可以下载.framework文件,将其拖到项目中,并使用一行代码进行初始化:[CintricFind initWithApiKey:@“YOUR_API_KEY_HERE”和secret:@“YOUR_SECRET_HERE”];

You can get an API key for free.

您可以免费获得一个API密钥。

There are docs here: https://www.cintric.com/docs/ios

这里有一些文档:https://www.cintric.com/docs/ios

And there is a blog post explaining further here: http://www.blog.cintric.com/?p=121

还有一篇博文对此作了进一步解释:http://www.blog.cintric.com/?