首页 > 解决方案 > 如何快速监控地点 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)
    }
}

标签: iosswiftcllocationmanager

解决方案


identifier您必须为要监控的每个区域使用唯一的。由于您使用的是常量,因此identifier每个区域都会替换前一个区域,因此您只需要监视最终区域。

我建议您使用该项目name作为区域identifier


推荐阅读