【问题标题】:Swift Locations: Fire every X Meters a Notification快速位置:每 X 米触发一次通知
【发布时间】:2016-11-07 13:25:01
【问题描述】:

我希望你们能帮助我,因为我的大脑因思考而燃烧:D。 我想通知用户,例如每 100 米。但我不想更改位置服务的刷新。有一个简单的方法,但我不喜欢它。我认为有更好的解决方案。 到目前为止我所做的(那是伪代码!)这在 didUpdateLocation 中调用

 //We want notify User but only 5 times during the movement
        if distanceToReport < settedDistance {
            //Store that first Notification is fired
            if !firstNotificationFired {
                self.notifyUser(report: report)

            }
            if firstNotificationFired && distanceToReport <= 400 {
                //Store that second Notification is fired
                self.notifyUser(report: report)
            } else if secondNotificationFired && distanceToReport <= 300 {
                //Store that third Notification is fired
            } else if thirdNotificationFired && distanceToReport <= 200 {
                //Store that fourth Notification is fired
            } else if fourthNotifiationFired && distanceToReport <= 100 {
                //Store that fifth Notification is fired
            }


        }

还有比这更好更有效的方法吗?或者有什么数学或算法可以提供帮助?

到目前为止谢谢。

【问题讨论】:

    标签: ios swift notifications location cllocationmanager


    【解决方案1】:

    清理代码的一种方法是将距离放入变量中并在每次通知时递增,应该减少 if 语句。

    if notificationShouldFire && distanceToReport <= targetDistance {
       self.notifyUser(report: report)
       targetDistance += targetDistance + 100
       notificationShouldFire = true
       // could also use count variable rather than a variable for each notif.
       firedNotifications += firedNotifications + 1
    }
    
    // your if statement would change slightly
    if firedNotifications <= 5 && distanceToReport <= targetDistance {
       // increment and notify here
    }
    

    如果你愿意,我认为你可以稍微提高效率,但你所拥有的并不是世界末日。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多