ios - 如何快速监控地点 4
问题描述
我在应用程序上工作,在该应用程序中我监控地点并在进入区域时发送本地通知。
我将这些地方保存在领域数据库中并检查区域。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Updated user's location")
shouldSetRegion = false
updateLocation()
print((locations.last?.coordinate.longitude)!)
if !(realm.objects(Coupon.self).isEmpty) {
for item in realm.objects(Coupon.self) {
let lat: CLLocationDegrees = CLLocationDegrees(item["lat"] as! String)!
let lng: CLLocationDegrees = CLLocationDegrees(item["lng"] as! String)!
center = CLLocationCoordinate2D(latitude: lat, longitude: lng)
region = CLCircularRegion(center: center, radius: 5000, identifier: "startPosition")
print(region)
manager.startMonitoring(for: region)
}
manager.startUpdatingLocation()
}
}
我使用此代码,尽管数据库包含多个位置,但他仅在用户输入数据库上的最后一个区域时才收到通知。
func updateLocation(){
shouldSetRegion = true
locationManager.startUpdatingLocation()
}
所以关于监控地点和发送本地通知的任何帮助。
@objc func didEnterRegion() {
print("da5alt hena")
for item in realm.objects(Coupon.self){
let title = item["name"] as! String
let body = item["desc"] as! String
let id = item["BranchId"] as! String
VKNotificationService.sharedInstance.locationRequest(title, body, for: id)
}
}
解决方案
identifier
您必须为要监控的每个区域使用唯一的。由于您使用的是常量,因此identifier
每个区域都会替换前一个区域,因此您只需要监视最终区域。
我建议您使用该项目name
作为区域identifier
推荐阅读
- python-3.x - Anaconda 安装在 Chromebook 中不起作用
- angular - 有没有办法从不同的目录获取组件?
- java - 尝试在空对象引用上调用虚拟方法“java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()”
- java - 获取传入类的属性到参数Java中
- java - 只想提取 $$text$$
- python - 如何在 python 中使用 pandas 合并表索引名称?
- javascript - 我无法从我的 csv 文件中获取列最大值。(Javascript D3)
- c# - unity 对象不平滑移动直线
- ns2 - 如何修复“无法读取文件”“:在ns2上执行“source.orig {}”时没有这样的文件或目录
- node.js - 如何在关联文档中获取值字段的总和