如何在Swift 3.0中计算用户到当前位置的行驶距离 [英] How to calculate the distance travelled by user from current location in swift 3.0

查看:79
本文介绍了如何在Swift 3.0中计算用户到当前位置的行驶距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UImapkit&核心位置框架
如何获得折线总距离和行程时间

I am using UImapkit & core location frameworks How will I get the total polyLine distance & travelled time

这是我的代码

func locationManager(_ manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {



      if let oldLocationNew = oldLocation as CLLocation?{
        let oldCoordinates = oldLocationNew.coordinate


        let newCoordinates = newLocation.coordinate
        var area = [oldCoordinates, newCoordinates]
        print(area)
        let polyline = MKPolyline(coordinates: &area, count: area.count)
        mkMapView.add(polyline)


    }

//calculation for location selection for pointing annoation
    if (previousLocation as CLLocation?) != nil{

        if previousLocation.distance(from: newLocation) > 10 {
            addAnnotationsOnMap(newLocation)
            previousLocation = newLocation
        }
    }else{
        //case if previous location doesn't exists
        addAnnotationsOnMap(newLocation)
        previousLocation = newLocation
    }
}


推荐答案

您可以使用 CLLocation 计算两个位置之间的距离。

You can use CLLocation to calculate the distance between two locations.

ie

let distance = newLocation.distance(from: oldLocation)

一旦计算出距离,便可以使用速度距离时间公式轻松计算出行车时间

Once you calculated the distance, then you can easily calculate the travel time by using the speed distance time formula

 speed = distance / time

因为您知道距离,并且假设速度,您可以计算出旅行所需时间

since you know the distance and if you assume the speed, you can calculate time taken for the travel as

 time = distance / speed

希望这玩意会妖you你。

Hope this stuff will help you.

这篇关于如何在Swift 3.0中计算用户到当前位置的行驶距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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