Tyler
Tyler

Reputation: 2386

CLLocation Manager how to update after certain distance

I am using CLLocationManager didupdatelocations like so:

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

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

}

This is all working fine but I only want to post the notification if the location is a certain distance away from the original location. Can I just use

locations.first

and compare that to locations.last seems like that would only update for the original not if the user continues moving around a city.

Upvotes: 3

Views: 852

Answers (1)

Icaro
Icaro

Reputation: 14845

To calculate the distance you will need two CLLocation (let's say, newLocation and oldLocation). You can calculate the distance between this two locations using:

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

After that just add you logic to decide when to post the notification:

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

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

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

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 this:

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 receive two locations and return the distance and any other needed information.

I hope that helps you!

Upvotes: 2

Related Questions