Distance from Current Location to Annotation. (Firebase)

236 Views Asked by At

I want to have a the distance from my Currentlocation to a annotation that's in the FireData Base. I tried to make it word but i can't ;(. I would like to have the distance between the two locations in a var. I hope you guys can help me.

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

Here is my Structure: Structure

2

There are 2 best solutions below

6
On BEST ANSWER

Try to use this code. Don't forget to enable your current location on map

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
On

I have used similar function, NOTE this was my function therefore it has rider and driver.. however you can change it to use annotation and location from 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"
                    }
                }

            }