could please give me example how calculate current "speed", i'm working on first simple app ala speedometer?
i figure out use didupdatetolocation
also found need use formula speed = distance / duration
is right? how calculate duration?
the basic steps need go through this:
- create cllocationmanager instance
- assign delegate appropriate callback method
- check see if "speed" set on cllocation in callback, if - there's speed
(optionally) if "speed" isn't set, try calculating distance between last update , current, divided difference in timestamps
import corelocation class locationdelegate: nsobject, cllocationmanagerdelegate { var last:cllocation? override init() { super.init() } func processlocation(_ current:cllocation) { guard last != nil else { last = current return } var speed = current.speed if (speed > 0) { print(speed) // or whatever } else { speed = last!.distance(from: current) / (current.timestamp.timeintervalsince(last!.timestamp)) print(speed) } last = current } func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) { location in locations { processlocation(location) } } } var del = locationdelegate() var lm = cllocationmanager(); lm.delegate = del lm.startupdatinglocation()
Comments
Post a Comment