iOS区域监控,监控20多个位置

iOS region monitoring, to monitor more than 20 locations

我进行了很多 google 搜索,但没有找到任何符合我需要的解决方案。 所以我想出了自己的解决方案,但不确定它是否可行。 我需要监控20多个地区。所以第一次,我将开始监控当前用户的位置,我得到 19 个我可以监控的其他可用位置,假设要监控的半径是 100 米。那么现在应该调用委托 didEnterRegion 吗?我真的不在乎这个。但是当用户从原始位置移动超过 100m 时,委托 didExitLocation 将被调用,此时,我将更新要监视的新区域(通过将新的当前用户位置发送到服务器,我将获得要监视的新区域列表),并且我仍然会监控这个新用户的位置,并且仍然会获得我可以监控的其他 19 个区域。 这个方案可行吗?有人试过吗? 如果应用挂起并且消耗大量电池,此解决方案是否仍然有效?

这实际上是Apple的建议Core Location Programming Guide:

To work around this limit, consider registering only those regions in the user’s immediate vicinity. As the user’s location changes, you can remove regions that are now farther way and add regions coming up on the user’s path.

但是,后台调用didEnterRegion:时,你得到多少时间还不清楚,所以不清楚如果运行后台你有时间调用服务器。 "significant-change location service" 信息说:

If you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives. At wake-up time, the app is put into the background and you are given a small amount of time (around 10 seconds) to manually restart location services and process the location data.... Because your app is in the background, it must do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your app will be terminated. If an iOS app needs more time to process the location data, it can request more background execution time using the beginBackgroundTaskWithName:expirationHandler: method of the UIApplication class.

您可以尝试结合区域监控,在 didEnterEter 区域进行服务器调用,然后调用 beginBackgroundTaskWithName:expirationHandler: 以确保您有足够的时间。不过,区域监控+服务器调用+后台处理的组合会影响电池寿命。

编辑:您还可以创建 "mega regions" 包含许多较小区域的大区域。当用户进入那些大型区域时,设置并添加您感兴趣的所有较小区域,当他们退出时,将它们移除。