Ios 计算核心位置swift的行程距离
我有一个像Uber应用程序一样计算旅行距离的应用程序。当驾驶员开始行程时,位置开始改变,即使在搜索乘坐过程中指定了起点,驾驶员也可以决定通过替代路线或经过较长的地点和路线,因为他/她不知道最短路线,那么我如何计算总距离 起始位置是指驾驶员点击开始按钮的位置 结束位置是驾驶员按下停止按钮的位置 这是到目前为止我的代码Ios 计算核心位置swift的行程距离,ios,swift,cllocationmanager,cllocation,cllocationdistance,Ios,Swift,Cllocationmanager,Cllocation,Cllocationdistance,我有一个像Uber应用程序一样计算旅行距离的应用程序。当驾驶员开始行程时,位置开始改变,即使在搜索乘坐过程中指定了起点,驾驶员也可以决定通过替代路线或经过较长的地点和路线,因为他/她不知道最短路线,那么我如何计算总距离 起始位置是指驾驶员点击开始按钮的位置 结束位置是驾驶员按下停止按钮的位置 这是到目前为止我的代码 public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.last!
endTrip(locations.last)
if !hasSetInitialLocation {
let camera = GMSCameraPosition.camera(withTarget: lastLocation!.coordinate, zoom: 17)
self.mapView.animate(to: camera)
hasSetInitialLocation = true
endTrip(lastLocation)
MqttManager.instance.connectToServer()
}
}
func endTrip(endLoaction: CLLocation) {
guard let statusChange = source.getStatusChange() else{return}
var distanceTraveled: Double = 0.0
let initialLocation = CLLocation(latitude: (statusChange.meta?.location?.lat)!, longitude: (statusChange.meta?.location?.lng)!)
let distance = initialLocation.distance(from: endLoaction)
distanceTraveled += distance
let distanceInKM = Utility.convertCLLocationDistanceToKiloMeters(targetDistance: distanceTraveled)
}
我如何计算距离以反映驾驶员移动的总距离,因为从拟定起点到终点的路线可能会发生变化 司机按下一个叫开始行程的按钮,我想知道从那个时刻到他按下结束行程按钮的那一刻的距离 这种实现可以从类似的工作代码中获得,但唯一的区别是,它们是在该点传递坐标的开始按钮和作为坐标终点的停止坐标
enum DistanceValue: Int {
case meters, miles
}
func calculateDistanceBetweenLocations(_ firstLocation: CLLocation, secondLocation: CLLocation, valueType: DistanceValue) -> Double {
var distance = 0.0
let meters = firstLocation.distance(from: secondLocation)
distance += meters
switch valueType {
case .meters:
return distance
case .miles:
let miles = distance
return miles
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if startLocation == nil {
startLocation = locations.first
} else if let location = locations.last {
runDistance += lastLocation.distance(from: location)
let calc = calculateDistanceBetweenLocations(lastLocation, secondLocation: location, valueType: .meters)
print("TOTAL LOC 1 \(calc)")
print("TOTAL LOC 2 \(runDistance)")
}
lastLocation = locations.last
}
如我的打印报表print(“LOC 1总计\(计算)”)所示
打印(“总LOC 2\(跑步距离)”)
我如何才能
calc
与runDistance
以下是控制台中打印的内容
TOTAL LOC 10.29331530774379
TOTAL LOC 2 10.29331530774379
TOTAL LOC 2.2655118031831587
TOTAL LOC 2 12.558827110926948
如果使用第一个和最后一个坐标获得这样的距离,它总是返回错误的值,因为它无法识别实际的行进路径 我使用以下代码解决了相同的问题。 使用谷歌地图
> pod 'GoogleMaps'
当驾驶员在路线上移动时,创建坐标数组
var arr = [Any]()
// Driving lat long co-ordinateds continues add in this array according to your expectation either update location or perticuler time duration.
// make GMSMutablePath of your co-ordinates
let path = GMSMutablePath()
for obj in arr{
print(obj)
if let lat = (obj as? NSDictionary)?.value(forKey: PARAMETERS.LET) as? String{
path.addLatitude(Double(lat)!, longitude: Double(((obj as? NSDictionary)?.value(forKey: PARAMETERS.LONG) as? String)!)!)
}
}
print(path) // Here is your traveling path
let km = GMSGeometryLength(path)
print(km) // your total traveling distance.
我在应用程序中完成了,它运行良好。
希望它能帮助你:)
或不使用谷歌地图
但是,根据代码,您必须为自己提供位置,一个CLLocationCoordinate2D数组
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// MARK: - Variables
let locationManager = CLLocationManager()
// MARK: - IBOutlet
@IBOutlet weak var mapView: MKMapView!
// MARK: - IBAction
@IBAction func distanceTapped(_ sender: UIBarButtonItem) {
let locations: [CLLocationCoordinate2D] = [...]
var total: Double = 0.0
for i in 0..<locations.count - 1 {
let start = locations[i]
let end = locations[i + 1]
let distance = getDistance(from: start, to: end)
total += distance
}
print(total)
}
func getDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
// By Aviel Gross
// https://stackoverflow.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
return from.distance(from: to)
}
}
类ViewController:UIViewController、CLLocationManagerDelegate、MKMapViewDelegate{
//标记:-变量
让locationManager=CLLocationManager()
//标记:-IBOutlet
@ibvar映射视图:MKMapView!
//标记:-IBAction
@iAction func distanceTapped(uu发送方:UIBarButtonItem){
let位置:[CLLocationCoordinate2D]=[…]
var总计:双倍=0.0
对于0..CLLocationDistance中的i{
//阿维埃尔·格罗斯
// https://stackoverflow.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points
let from=CLLocation(纬度:from.lation,经度:from.longitude)
let to=CLLocation(纬度:至。纬度,经度:至。经度)
返回距离(从:到)
}
}
输出
一个简单的函数,用于计算给定CLLocationCoordinate2D数组的距离(以米为单位)。使用
reduce
代替数组迭代
func computeDistance(from points: [CLLocationCoordinate2D]) -> Double {
guard let first = points.first else { return 0.0 }
var prevPoint = first
return points.reduce(0.0) { (count, point) -> Double in
let newCount = count + CLLocation(latitude: prevPoint.latitude, longitude: prevPoint.longitude).distance(
from: CLLocation(latitude: point.latitude, longitude: point.longitude))
prevPoint = point
return newCount
}
}
但是你的问题是什么?我如何计算距离以反映驾驶员移动的总距离?这不只是连接位置点并使用
CLLocationDistance
和CLLocationCoordinate2D
计算整个距离的问题吗?我想你需要经常检查当前位置并进行比较添加到上一个,并将距离添加到total@ElTomato但是,如果你绕街区转一圈,行程有多远?也许是一个哲学问题:)?@King Most Welcome亲爱的,你的问题也值得+1:)谢谢。这个解决方案适用于人们可能遇到的许多问题have@King是的,肯定是:)