2017-02-12 5 views
0

私はユーザーの現在地と特定の場所を持っています。私はマイルで距離を取得しようとしています。私の距離はマイルでは間違っているようだ、私はアプリで1.2マイルを得て、次に私はGoogleマップでマイルをチェックした。私はグーグルマップで2.1マイルを得た。ここで私のコードは、私はなぜ私のコードが正確ではないか分からない。助けてください。私の距離はマイル計算で間違っているようです3

var locationManager = CLLocationManager() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    map.delegate = self 
    map.userTrackingMode = MKUserTrackingMode.follow 

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

    // set location 
    let latitude: CLLocationDegrees = 32.5850 
    let longitude: CLLocationDegrees = -85.4904 
    let latDelta: CLLocationDegrees = 0.05 
    let lonDelta: CLLocationDegrees = 0.05 
    let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta) 
    let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
    let region = MKCoordinateRegion(center: coordinates, span: span) 

    map.setRegion(region, animated: true) 

    // add annotation 
    let annotation = MKPointAnnotation() 
    annotation.title = "1100 S College St" 
    annotation.coordinate = coordinates 

    map.addAnnotation(annotation) 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    let userLocation: CLLocation = locations[0] 
    let latitude = userLocation.coordinate.latitude 
    let longitude = userLocation.coordinate.longitude 

    let location1 = CLLocation(latitude: 32.5850, longitude: -85.4904) // set location 

    let location2 = CLLocation(latitude: latitude, longitude: longitude) // user's current location 

    let distanceInMeters : CLLocationDistance = location1.distance(from: location2) // distance in meters 

    let distanceInMiles = distanceInMeters/1609.344 // distance in miles 

    let roundUp = String(format:"%.1f", distanceInMiles) // round up 

    distanceLabel.text = "\(roundUp) mi" 

} 

答えて

0

この部分は正しいです:メートルで

let distanceInMiles = distanceInMeters/1609.344 // distance in miles 

チェック距離とGoogleマップとの比較。 あなたがiOS 10にいる場合は、このような新しいMeasurementおよび関連クラスを使用することができます。

let meters = 1000.0 

let distanceMeters = Measurement(value: meters, unit: UnitLength.meters) 

let distanceMiles = distanceMeters.converted(to: UnitLength.miles) 
let miles = distanceMeters.converted(to: UnitLength.miles).value 

このコードは、この結果を生成します。 enter image description here

+0

こんにちは、ちょうど私をメートルとキロメートルで私の距離をチェックし、Google Mapsと比較しました。私は別の番号を得た。私は距離番号を取得するときに私は間違っていたか分からないのですか? – QQ10

+0

'iOS'と' Google Maps'の 'location1'と' location2'の正確な座標で距離を確認してください – mhergon

+0

私はちょうどlocation1とlocation2の座標をチェックしました。 Googleマップに表示されている座標はあまり正確ではないと思います。なぜ私は正確に座標をコピーし、私のアプリに適用されます。私は私のアプリで2.1マイルを得た。 (私がGoogleマップに載せたのと同じ番号)。しかし、location1(location1アイコン)とlocation2(ユーザーの場所)が正しく表示されません。正しい座標があるかどうかを確認する良い方法はありますか? – QQ10

0

スウィフト4、XCode9

extension CLLocation { 

    func myDistance(coordinate: CLLocation)->String{ 

     let distanceInMeters = self.distance(from: coordinate) 

     if distanceInMeters < 1000 { 
      return distanceInMeters.description + " M" 
     }else{ 
      let distanceInKilometer = distanceInMeters/1000 
      return String(format:"%.1f", distanceInKilometer) + " KM" 
     } 

    } 

} 
関連する問題