2016-08-07 3 views
2

私は2つの座標間の距離を計算しようとしています。これは私が行う:なぜiOSのCLLocationに間違った値が表示されるのですか?

func setupLocationManager() { 
    if CLLocationManager.authorizationStatus() == .NotDetermined { 
     locationManager.requestWhenInUseAuthorization() 
    } 

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 
} 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let latitude = locations.last?.coordinate.latitude 
    let longitude = locations.last?.coordinate.longitude 

    let myLocation = CLLocation(latitude: latitude!, longitude: longitude!) 
    let targetLocation = CLLocation(latitude: 41.4381022, longitude: 46.604910) 

    let distance = myLocation.distanceFromLocation(targetLocation) 
    print(distance) 
} 

私はチェックしている13 kmの距離です!しかし、私のアプリは私に2〜3kmを示しています!

どうすれば改善できますか?

+0

あなたのメソッドに渡されている緯度と経度は何ですか?これは私にとって正しいと思われる。 – Arclite

+0

手で座標を1つ置き、自分の位置を検出します。そして2つの場所の間の距離を計算したい@Arclite – Doe

+0

どのようにGoogleマップでチェックしていますか? –

答えて

5

Googleマップは、2つの場所の間のルート距離を示します。CLLocationは、2つの場所の間の鳥瞰距離を示します。 documentationから

この方法は、地球の曲率を、以下、それらの間 ラインをトレースすることにより2つの位置の間の距離を測定します。 結果として生じる円弧は滑らかな曲線であり、2つの場所の間で特定の標高の変化が考慮されません( )。

0

ここにはworking GPS appに基づく例があります。

import CoreLocation 

public class SwiftLocation: NSObject, CLLocationManagerDelegate { 

    private let locationManager = CLLocationManager() 
    private var latestCoord: CLLocationCoordinate2D 

    init(ignore:String) { 

     locationManager.requestAlwaysAuthorization() 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.distanceFilter = kCLDistanceFilterNone 
     locationManager.startUpdatingLocation() 
     latestCoord = locationManager.location!.coordinate 

     super.init() 

     locationManager.delegate = self 
    } 

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

     latestCoord = manager.location!.coordinate 

    } 

    public func getLatest() -> CLLocationCoordinate2D { 
     return latestCoord 
    } 
} 
関連する問題