2017-09-15 15 views
0

私は自分のCurrentlocationからFireData Baseの注釈までの距離を持っています。私はそれを言葉作ってみましたが、私がすることはできません。(私はVarの2つの位置の間の距離を持っているしたいと思い、私はあなたたちが私を助けることができると思います現在の場所から注釈までの距離。 (Firebase)

func reload(){ 
    //get data 
    Database.database().reference().child("Rollerbanken").observe(.value, with: { (snapshot) in 



     for item in snapshot.children{ 

      if let value = snapshot.value as? Dictionary<String, Any> { 

       for key in value.keys { 
        if let itemDict = value[key] as? Dictionary<String, AnyObject> { 

         let annotation = MKPointAnnotation() 
         annotation.title = itemDict["TypeControle"] as! String 
         let tijd = itemDict["Tijd"] as! String 
         annotation.subtitle = "Geplaatst om \(tijd)" 

         let getLatitude = itemDict["Latitude"] as? String 
         let getLongitude = itemDict["Longitude"] as? String 
         if let lat = getLatitude, let long = getLongitude { 
          annotation.coordinate = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!) 

          self.map.addAnnotation(annotation) 

          let directionRequest = MKDirectionsRequest() 
          directionRequest.source = MKMapItem.forCurrentLocation() 
          if #available(iOS 10.0, *) { 
           directionRequest.destination = MKMapItem(placemark: MKPlacemark.init(coordinate: CLLocationCoordinate2DMake(Double(lat)!, Double(long)!))) 
          } else { 
           // Fallback on earlier versions 
          } 
          directionRequest.transportType = .walking 
          let direction = MKDirections(request: directionRequest) 
          direction.calculate(completionHandler: { (response, error) in 
           if error != nil { 
            print("Error while build route") 
           } else { 
            let route = response?.routes.last 
            let distance = route?.distance 
            print(distance) 
           } 
          }) 
         } 
        } 
       } 
      } 
     } 
    }) 
} 

ここに私の構造である:。。。 Structure

答えて

0

このコードを使用してみてください。これは、それはライダーやドライバーを持っているので、私の関数であった注意、私は同様の機能を使用しているマップ

let directionRequest = MKDirectionsRequest() 
     directionRequest.source = MKMapItem.forCurrentLocation() 
     directionRequest.destination = MKMapItem(placemark: MKPlacemark.init(coordinate: CLLocationCoordinate2DMake(YOURPOINTLATITUDE, YOURPOINTLONGITUDE))) 
     directionRequest.transportType = .walking 
     let direction = MKDirections(request: directionRequest) 
     direction.calculate(completionHandler: { (response, error) in 
      if error != nil { 
       print("Error while build route") 
      } else { 
       let route = response?.routes.last 
       let distance = route?.distance 
+0

あなたのメソッド(更新されたコードを見てください)を使用しましたが、それでも機能しません。 –

+0

@DaniKemper確かに、あなたはfirebaseから正しい座標を得て、地図上にあなたの現在の場所を示すために地理位置を可能にしますか? これをもっと使いましょう getLongitude =(itemDict ["Longitude"]をNSStringとします).doubleValue より正確 – maxkoriakin

+0

はい私はポジティブです! –

0

であなたの現在の場所を有効にすることを忘れないでください...しかし、あなたは、注釈と場所を使用するように変更することができますfirebase。

if let rideRequestDictionary = snapshot.value as? [String:AnyObject] { 

      // Getting the rider location and email 
      if let email = rideRequestDictionary["email"] as? String { 
       if let lat = rideRequestDictionary["lat"] as? Double{ 
        if let lon = rideRequestDictionary["lon"] as? Double{ 

         // Getting the Driver location and email 
         let driverCLLocation = CLLocation(latitude: driverLocation.latitude, longitude: driverLocation.longitude) 
         let riderCLLocation = CLLocation(latitude: lat, longitude: lon) 

         // getting the distance between the two people 
         let distance = driverCLLocation.distance(from: riderCLLocation)/1000 

         // rounding the distances 
         let roundedDistance = round(distance * 100)/100 

         // putting the rounded distance and email in label 
         cell.textLabel?.text = "\(email) - \(roundedDistance)km away" 
        } 
       } 

      } 
関連する問題