【问题标题】:How to calculate the distance travelled by user from current location in swift 3.0如何在swift 3.0中计算用户从当前位置行进的距离
【发布时间】:2017-07-03 04:38:01
【问题描述】:

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

这是我的代码

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
    }
}

【问题讨论】:

    标签: swift core-location


    【解决方案1】:

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

    let distance = newLocation.distance(from: oldLocation)
    

    计算出距离后,使用速度距离时间公式即可轻松计算行程时间

     speed = distance / time
    

    因为你知道距离,如果你假设速度,你可以计算旅行所花费的时间

     time = distance / speed
    

    希望这些东西对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 2019-10-07
      • 2018-01-26
      • 2012-10-05
      • 2012-01-16
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多