CLLocation manager how to update after a certain distance

I am using CLLocationManager didupdatelocations like this:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    location = locations.last as? CLLocation

    NSNotificationCenter.defaultCenter().postNotificationName("location", object: self)

}

      

Everything works fine, but I only want to post a notification if the location is some distance from the original location. Can i just use

locations.first

      

and compare that to location.last. It seems like this will only update for the original unless the user navigates around town.

+3


source to share


1 answer


To calculate the distance, you need two CLLocation

(say newLocation

and oldLocation

). You can calculate the distance between these two locations using:

let distance = Double(newLocation.distanceFromLocation(oldLocation))

      

After that, just add logic to decide when to send the notification:

if distance > myMinimum distance{
    NSNotificationCenter.defaultCenter().postNotificationName("location", object: self)
}

      

Note , this is the shortest distance calculated between points (straight line), it does not calculate route distance.

If you want to calculate the distance of a route between two points, you need to use MKDirectionsRequest, this will return you one or more routes from point A to point B with step by step instructions:



class func caculateDistance(){
    var directionRequest = MKDirectionsRequest()
    var sourceCoord = CLLocationCoordinate2D(latitude: -36.7346287, longitude: 174.6991812)
    var destinationCoord = CLLocationCoordinate2D(latitude: -36.850587, longitude: 174.7391745)
    var mkPlacemarkOrigen = MKPlacemark(coordinate: sourceCoord, addressDictionary: nil)
    var mkPlacemarkDestination = MKPlacemark(coordinate: destinationCoord, addressDictionary: nil)
    var source:MKMapItem = MKMapItem(placemark: mkPlacemarkOrigen)
    var destination:MKMapItem = MKMapItem(placemark: mkPlacemarkDestination)
    directionRequest.setSource(source)
    directionRequest.setDestination(destination)
    var directions = MKDirections(request: directionRequest)
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void in
        if error != nil { println("Error calculating direction - \(error.localizedDescription)") }
        else {
            for route in response.routes{
                println("Distance = \(route.distance)")
                for step in route.steps!{
                    println(step.instructions)
                }  
            }
        }
    }
}

      

This example code will return you the following:

Disntance
Distance = 16800.0

Step by Step instructions
Start on the route
At the end of the road, turn left onto Bush Road
Turn right onto Albany Expressway
At the roundabout, take the first exit onto Greville Road toward 1, Auckland
At the roundabout, take the third exit to merge onto 1 toward Auckland
Keep left
Take exit 423 onto Shelly Beach Road
Continue onto Shelly Beach Road
At the end of the road, turn right onto Jervois Road
Turn left onto Islington Street
Keep right on Islington Street
Arrive at the destination

      

The function can be easily modified to get two locations and return distance and any other information you need.

I hope this helps!

+2


source







All Articles