ios - CLLocation Manager how to update after certain distance -


i using cllocationmanager didupdatelocations so:

func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) {     location = locations.last as? cllocation      nsnotificationcenter.defaultcenter().postnotificationname("location", object: self)  } 

this working fine want post notification if location distance away original location. can use

locations.first 

and compare locations.last seems update original not if user continues moving around city.

to calculate distance need 2 cllocation (let's say, newlocation , oldlocation). can calculate distance between 2 locations using:

let distance = double(newlocation.distancefromlocation(oldlocation)) 

after add logic decide when post notification:

if distance > myminimum distance{     nsnotificationcenter.defaultcenter().postnotificationname("location", object: self) } 

note, shortest distance calculated between points (straight line) not calculate route distance.

if want calculate route distance between 2 points need use mkdirectionsrequest, return one, or many, routes point point b step 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 {             route in response.routes{                 println("distance = \(route.distance)")                 step in route.steps!{                     println(step.instructions)                 }               }         }     } } 

this example code return this:

disntance distance = 16800.0  step step instructions start on route @ end of road, turn left onto bush road turn right onto albany expressway @ roundabout, take first exit onto greville road toward 1, auckland @ roundabout, take third exit merge onto 1 toward auckland keep left take exit 423 onto shelly beach road continue onto shelly beach road @ end of road, turn right onto jervois road turn left onto islington street keep right on islington street arrive @ destination 

the function can modified receive 2 locations , return distance , other needed information.

i hope helps you!


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -