2017-09-15 90 views
0

我想要從我的Currentlocation到FireData Base中的註釋的距離。 。我試圖使它字,但我不能(我想有一個變種這兩個位置之間的距離,我希望你們能幫助我從當前位置到註釋的距離。 (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 [ 「經度」]作爲?的NSString).doubleValue 更加精確 – maxkoriakin

+0

是的,我positieve! –

0

我已經使用了類似的功能,請注意,這是我的,因此它的功能有騎手和驅動程序。但是你可以將其更改爲使用註釋和位置火力點。

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" 
        } 
       } 

      } 
相關問題