Search code examples
iosswiftcllocationcllocationdistance

Convert distanceFromLocation to Int


I'm sure there is an obvious solution to this issue, but I've been trying different things for a while now and I can't figure it out. Basically I have got the distance between two points, and I am trying to convert it from a Double to an Int (to remove all decimal points), however everything I have tried hasn't worked.

This is my code:

let userLocation = CLLocation(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
let annotationLocation = CLLocation(latitude: latitudePassed, longitude: longitudePassed)

var distance = CLLocationDistance(annotationLocation.distanceFromLocation(userLocation))

if distance > 1000 {
    distance = distance / 1000
    rideAnnotation.subtitle = "\(distance) kilometer(s)"
} else {
    rideAnnotation.subtitle = "\(distance) meters"
}

I've tried converting to a string then an int, converting to an int with toInt() and a few other things, but to no success.

Anyone have any suggestions?


Solution

  • It's possible I'm missing something, but does let distanceInt = Int(distance) not do exactly what you want?