ios - Swift - Return value from locationManager function in class extension -
i'm trying directions users current location destination using google maps. want done when showdirection button pressed, can't figure how return or pass users location ibaction function func locationmanager(... didupdatelocation) ibaction doesn't use parameters in can pass locvalue to.
here showdirection button function:
@ibaction func showdirection(sender: anyobject) { print("running showdirection") let instanceone = parseviewcontroller() // create parseviewcontroller instance operate on print("created parseview instance") let coord = instanceone.returnparse() let latitude = (coord.lat nsstring) let longitude = (coord.long nsstring) var urlstring = "http://maps.google.com/maps?" urlstring += "saddr= // users location didupdatelocation" urlstring += "&daddr= \(latitude string), \(longitude string)" print(urlstring) if let url = nsurl(string: urlstring.stringbyaddingpercentencodingwithallowedcharacters(nscharacterset.urlqueryallowedcharacterset())! { uiapplication.sharedapplication().openurl(url) } }
and here locationmanager function locvalue:
func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { if let location = locations.first { mapview.camera = gmscameraposition(target: location.coordinate, zoom: 15, bearing: 0, viewingangle: 0) let locvalue:cllocationcoordinate2d = (manager.location?.coordinate)! print("coordinates = \(locvalue.latitude), \(locvalue.longitude)") locationmanager.stopupdatinglocation() } }
any appreciated!
you need create internal variable in class store location if want use in function. e.g.
class yourviewcontroller: uiviewcontroller ... { var lastlocation: cllocation? = nil ... }
in didupdatelocations
:
if let location = locations.first { lastlocation = location ... }
and can access in func showdirection()
Comments
Post a Comment