计算iOS Swift的总行进距离 [英] Calculate Total Traveled Distance iOS Swift

查看:134
本文介绍了计算iOS Swift的总行进距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算在Swift中使用CoreLocation的总行程距离

How can I calculate the total distance traveled use CoreLocation in Swift

到目前为止,我还未能找到任何有关如何在Swift中执行此操作的资源iOS 8,

I haven't been able to so far find any resources for how to do this in Swift for iOS 8,

您如何计算自开始追踪您的位置以来移动的总距离?

How would you calculate the total distance moved since you began tracking your location?

从什么我到目前为止读过,我需要保存点的位置,然后计算当前点和最后一点之间的距离,然后将该距离添加到totalDistance变量

From what I've read so far, I need to save location of a points, then calculate the distance between current point, and last point, then add that distance to a totalDistance variable

Objective-C对我来说非常陌生,所以我无法解决swift语法

Objective-C is extremely unfamiliar to me, so I haven't been able to work out the swift syntax

这是我到目前为止所做的,而不是确定我做得对。虽然 distanceFromLocation 方法返回全部0.0,但很明显出错了

Here is what I've worked out so far, not sure if I'm doing it right. Though the distanceFromLocationmethod is returning all 0.0 so obviously something is wrong

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
     var newLocation: CLLocation = locations[0] as CLLocation

    oldLocationArray.append(newLocation)
           var totalDistance = CLLocationDistance()
    var oldLocation = oldLocationArray.last

    var distanceTraveled = newLocation.distanceFromLocation(oldLocation)

    totalDistance += distanceTraveled

 println(distanceTraveled)



}


推荐答案

更新: Xcode 8.3.2•Swift 3.1

问题出在那里因为你总是一遍又一遍地获得相同的位置。试试这样:

The problem there is because you are always getting the same location over and over again. Try like this:

import UIKit
import MapKit

class ViewController: UIViewController,  CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    let locationManager = CLLocationManager()
    var startLocation: CLLocation!
    var lastLocation: CLLocation!
    var startDate: Date!
    var traveledDistance: Double = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
            locationManager.startMonitoringSignificantLocationChanges()
            locationManager.distanceFilter = 10
            mapView.showsUserLocation = true
            mapView.userTrackingMode = .follow
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if startDate == nil {
            startDate = Date()
        } else {
            print("elapsedTime:", String(format: "%.0fs", Date().timeIntervalSince(startDate)))
        }
        if startLocation == nil {
            startLocation = locations.first
        } else if let location = locations.last {
            traveledDistance += lastLocation.distance(from: location)
            print("Traveled Distance:",  traveledDistance)
            print("Straight Distance:", startLocation.distance(from: locations.last!))
        }
        lastLocation = locations.last
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        if (error as? CLError)?.code == .denied {
            manager.stopUpdatingLocation()
            manager.stopMonitoringSignificantLocationChanges()
        }
    }
}

示例项目

这篇关于计算iOS Swift的总行进距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆