2017-05-31 7 views
-2

私の詳細ビューにラベルがあります。ユーザーが移動すると現在のユーザーの場所から特定の注釈までの距離を計算します。
そして、そのラベルに距離を表示
例えば:「あなたの場所から3.6キロ」ユーザーの場所から注釈までの距離を計算してラベルに表示する方法

+0

[座標間の距離を調べる方法は?](https://stackoverflow.com/questions/33304340/how-to-find-out-distance-between-coordinates) –

答えて

0

あなたの注釈は、locationプロパティを持つことになります。 coordinateOne.distance(から:coordinateTwo)= distanceInMeters

を聞かせて - 点間の距離うち二つの作品は、あなたがより多くの情報については、このリンクを読むことができます

-

How to find out distance between coordinates?

1

を私は拡張子を使用することをお勧めします。注釈用の独自のクラスを持っているとします(そうでない場合は、 "YourAnnotationClass"の代わりにMKA​​nnotationを使用します)。計算されたプロパティdistanceToUsersCurrentLocationで拡張します。明らかに、ユーザーの現在地にアクセスするには、CoreLocationをインポートする必要があります。

import UIKit 
import CoreLocation 

public class ViewController { //class where you need to set the label's text 

    //your code here 

    } 
} 

extension YouAnnotationClass { 
    var distanceToUsersCurrentLocation: Double { 
     let manager = CLLocationManager() //location manager for user's current location 
     let destinationCoordinates = CLLocation(latitude: self.latitude, longitude: self.longitude) //coordinates for destinastion 
     let selfCoordinates = CLLocation(latitude: (manager.location?.coordinate.latitude)!, longitude: (manager.location?.coordinate.longitude)!) //user's location 
     return selfCoordinates.distance(from: destinationCoordinates) //return distance in **meters** 
} 

毎回あなたがこのクラス内distanceToUsersCurrentLocationにアクセスするには、メートル単位で距離を取得します。これがあなたの問題に役立つことを願っています

関連する問題