【问题标题】:How to Monitor places swift 4如何快速监控地点 4
【发布时间】:2019-01-12 13:50:23
【问题描述】:

我在应用程序上工作,我在该应用程序上监控地点并在进入区域时发送本地通知。

我将这些地方保存在领域数据库中并检查区域。

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)
    }
}

【问题讨论】:

    标签: ios swift cllocationmanager


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多