swift - Why i can't get Location? -


i want location coordinates. , reason loactionmanger:didupdatelocations not work. hope can help.

import corelocation class location: nsobject , cllocationmanagerdelegate {     let locationmanager = cllocationmanager()     var latitude = 0.0     var longitude = 0.0     func getlocation() -> (latitude:double , longitude: double ){         if(cllocationmanager.locationservicesenabled()){             locationmanager.delegate = self             locationmanager.desiredaccuracy = kcllocationaccuracykilometer             locationmanager.requestwheninuseauthorization()             locationmanager.requestalwaysauthorization()             locationmanager.startupdatinglocation()         }         return (latitude, longitude)     }     func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) {         var location :cllocationcoordinate2d = manager.location.coordinate         latitude = location.latitude         longitude = location.longitude         println("location: latitude: \(latitude)")         println("location: longitude: \(longitude)")         locationmanager.stopupdatinglocation()     }     func locationmanager(manager: cllocationmanager!, didfailwitherror error: nserror!) {         println("error while updating location" + error.localizeddescription)     } } 

here example works. check various authorizations , errors framework can return.

one thing keep in mind: object implementing cllocationmanagerdelegate needs retained. object gets deinit, goes delegate call stop.

usage: ctor create object, call startobserving() , stopobserving() in client if stream location data in delegate method (streaming code not provided). use getcurrentlocation() 1 time value.

import corelocation  let default_location_accuracy = kcllocationaccuracyhundredmeters  struct locationoptions {     var accuracy: double      init(enablehighaccuracy: bool = true) {         accuracy = enablehighaccuracy ? kcllocationaccuracybest : default_location_accuracy     } }  let loc = locationoptions(enablehighaccuracy: false)  class locationobserver: nsobject, cllocationmanagerdelegate {      var _locationmanager = cllocationmanager()     var _observinglocation: bool?     var _observeroptions: locationoptions?      // lifecycle      init(accuracy: bool = true) {         super.init()         _locationmanager.delegate = self         _locationmanager.distancefilter = default_location_accuracy         _observeroptions = locationoptions(enablehighaccuracy: accuracy)     }      deinit {         _locationmanager.stopupdatinglocation()         _locationmanager.delegate = nil     }      // private api      private func beginlocationupdates() {          // request location access permission         _locationmanager.requestwheninuseauthorization()          // start observing location         _locationmanager.startupdatinglocation()     }      // public api      func startobserving() {          _locationmanager.desiredaccuracy = _observeroptions!.accuracy         beginlocationupdates()         _observinglocation = true     }      func stopobserving() {         // stop observing         _observinglocation = false          // stop updating if no pending requests         _locationmanager.stopupdatinglocation()     }      func getcurrentposition(options: locationoptions) {          if !cllocationmanager.locationservicesenabled() {             nslog("location services disabled.")             return         }          if cllocationmanager.authorizationstatus() == .denied {             return         }          // configure location manager , begin updating location         _locationmanager.desiredaccuracy = min(_locationmanager.desiredaccuracy, options.accuracy)         beginlocationupdates()     }      // cllocationmanagerdelegate     func locationmanager(manager: cllocationmanager, didupdatelocations locations: [anyobject]) {          // location data         let location = locations[locations.count - 1] as! cllocation          // latitude:         location.coordinate.latitude,         // longitude:        location.coordinate.longitude,         // altitude:         location.altitude,         // accuracy:         location.horizontalaccuracy,         // altitudeaccuracy": location.verticalaccuracy,         // heading:          location.course,         // speed:            location.speed,          //         // store location data inside structure here.         //           // stop updating if not observing         if !(_observinglocation!) {             _locationmanager.stopupdatinglocation()         }          // reset location accuracy         _locationmanager.desiredaccuracy = default_location_accuracy     }  }  // usage let locobs = locationobserver() // location observer high accuracy default  locobs.startobserving() // start observing  // gather data inside cllocationmanagerdelegate locationmanager:didupdatelocations: method  locobs.stopobserving() // stop observing   // alternative start/stop. 1 time location low accuracy (for example) locobs.getcurrentposition(locationoptions(enablehighaccuracy: false)) 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -