2017-07-29 8 views
1

私は2つの座標を取ってお互いに一致させてボタンがポップアップするようにしようとしていますが、私はエラーを受け取り続けます。ここに私のコードは、これまでのところです:どのように2つの座標を一致させるか?

var userLocation: CLLocationCoordinate2D? 
var driverLocation: CLLocationCoordinate2D? 

func payTime() { 
     if driverLocation == userLocation { 
      payNowButton.isHidden = false 
     } 
    } 

私はあなたがお互いに自分の緯度と長いを確認することができる2つのCLLocationCoordinate2Dsを比較するためにスウィフト3、Firebase、とXcode 8

+3

ここでエラーは何ですか? – NRitH

答えて

2

を使用しています。

func payTime() { 
    if driverLocation?.latitude == userLocation?.latitude && driverLocation?.longitude == userLocation?.longitude { 
     // Overlapping 
    } 
} 

ただし、これはまったく同じ場所にある場合にのみ有効です。あるいは、次のようなものを使用することができます。

func payTime() { 
    if let driverLocation = driverLocation, let userLocation = userLocation{ 
     let driverLoc = CLLocation(latitude: driverLocation.latitude, longitude: driverLocation.longitude) 
     let userLoc = CLLocation(latitude: userLocation.latitude, longitude: userLocation.longitude) 
     if driverLoc.distance(from: userLoc) < 10{ 
      // Overlapping 
     } 
    } 
} 

これは、2つのポイントをCLLocationに変換してから、どれくらい離れているかをメートルでチェックします。あなたは望みの結果を得るために閾値で遊ぶことができます。

編集1:ここでは

は、それが簡単に簡単に場所を比較できるようにする拡張機能です。

extension CLLocationCoordinate2D{ 
    func isWithin(meters: Double, of: CLLocationCoordinate2D) -> Bool{ 
     let currentLoc = CLLocation(latitude: self.latitude, longitude: self.longitude) 
     let comparingLoc = CLLocation(latitude: of.latitude, longitude: of.longitude) 
     return currentLoc.distance(from: comparingLoc) < meters 
    } 
} 

func payTime() { 
    if let driverLocation = driverLocation, let userLocation = userLocation{ 
     if driverLocation.isWithin(meters: 10, of: userLocation){ 
      // Overlapping 
     } 
    } 
} 
+1

このような質の低い質問は、あなたのような高品質の回答に値するものではありません。 (投票しました)次のステップは、 'CLLocationCoordinate2D'または' CLLocation'の拡張モジュールisWithin(meters:of:)を提案することです。 –

+0

@DuncanC Thats素晴らしいアイデアです!拡張で質問を編集します。 –

+0

また、 'CLLocationCoordinate2D'を拡張可能にする拡張を追加します – Alexander

関連する問題