1

I'm a newbie in swift and I'm trying to calculate the distance, on route, from userLocation to several Point of interest.

I don’t want, in this part of the app, “draw” the route on a map, but I only want to calculate the distance on route instead the distance between two coordinate and then show this distance as information inside the callout on the map.

The coordinate of the POI (latitude and longitude) are contained in a database.

I have read some threads about this argument here on Stack Overflow:

Measuring distance in meters from a drawn route in MKMapView

MKMapView get distance between coordinates on customized route

and other tutorials:

http://www.technetexperts.com/mobile/draw-route-between-2-points-on-map-with-ios7-mapkit-api/ https://videos.raywenderlich.com/courses/mapkit-and-core-location/lessons/9

then i wrote this code:

for item in items {
if item.latitudine != "" && item.longitudine != "" {

// Point Of Interest coordinate
let latitude = Double(item.latitude!)
let longitude = Double(item.longitude!)
let itemLocation = CLLocation(latitude: latitude!, longitude: longitude!)
let itemLocationPlacemark = MKPlacemark(coordinate: itemLocation.coordinate, addressDictionary: nil)

// user coordinate
let userLocation = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
let userLocationPlacemark = MKPlacemark(coordinate: userLocation.coordinate, addressDictionary: nil)

// create Request object
 let request = MKDirectionsRequest()
 request.source = MKMapItem(placemark: userLocationPlacemark)
 request.destination = MKMapItem(placemark: itemLocationPlacemark)
 request.requestsAlternateRoutes = false
 request.transportType = .automobile


 let directions = MKDirections(request: request)
 directions.calculate {
 [weak self] (response, error) in
     if error == nil {
         for route in (response?.routes)! {
         let distance = (route.distance)/1000
         print(distance)                            
                        }
                    }
                }
            }
    }

The problem is when I execute the code from the line directions.calculate.

The program run the line but then don’t execute the rest, don’t execute the control if error == nil and the instructions in the closure.

So now I wonder if my idea is wrong and, if not, how can obtain my goal.

Community
  • 1
  • 1
Giuseppe Sala
  • 383
  • 2
  • 4
  • 16
  • Take a look at this tutorial: https://www.devfright.com/mkdirections-tutorial/ The tutorial includes more detail on processing the results from the MKDirections request – Ron Diel Dec 30 '16 at 01:33
  • thank you very much for your answer i'll try to understand the tutorial but i don't know obj c – Giuseppe Sala Dec 30 '16 at 16:02
  • No problem. I don't either but the API calls can be pretty useful as examples – Ron Diel Dec 30 '16 at 19:07

1 Answers1

0

(Posted solution on behalf of the OP).

Reading other threads I understand that the problem was the closure inside che for loop. So after several attempts I found the solution that work for me. I write it here so that can be useful to someone else:

var counter: Int!
...

for item in itemPin {

            if item.latitude != "" && item.longitude != "" {
....
....
let directions = MKDirections(request: request)
directions.calculate {
                    (response, error) in
                    print("error \(error)")
                    if error == nil {
                    counter = self.counter + 1
                        print(self.counter)
                        if self.counter >= self.itemPin.count {
                        let result = (response?.routes[0].distance)!/1000
                        }
                    }
                 }
...
         }
}
halfer
  • 19,824
  • 17
  • 99
  • 186