2017-09-15 92 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" 
        } 
       } 

      } 
相关问题