2017-07-03 1 views
0

に現在の場所からユーザーが移動した距離を計算する方法 がどのように私は総ポリライン距離&は時間は私がUImapkit &コアの場所を使用していますSWIFT 3.0

この私のコード

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

答えて

2
を旅し得るでしょうフレームワーク

CLLocationを使用すると、2つの場所間の距離を計算できます。

let distance = newLocation.distance(from: oldLocation) 

すなわち

あなたが距離を計算したら、あなたが想定した場合、あなたは距離を知っているし、以来、あなたは簡単にスピード、距離、時間式

speed = distance/time 

を使用して旅行時間を計算することができますがあなたは旅行にかかる時間を計算することができます。

time = distance/speed 

ホープティあなたのものがあなたを助けます。

関連する問題