AKLocationManager
is class which can better controll your location.
- Detect first location.
- Updating locaiton after time interval.
- Updating location when you moving, disable when you stop.
- Connection to Google Map location manager.
- Play / Pause updating locaiton.
- Better error management.
var locationManager: AKLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = AKLocationManager()
locationManager.startUpdatingLocationWithRequest()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
locationManager.stopUpdatingLocation()
}
var locationManager: AKLocationManager! {
didSet { locationManagerger.delegate = self }
}
var mapView: GMSMapView!
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
locationManager = AKLocationManager()
mapView = GMSMapView()
mapView.alpha = 0
mapView.frame = view.frame
view.addSubview(mapView)
view.sendSubviewToBack(mapView)
locationManager.addObserver(mapView, forKeyPath: "myLocation")
locationManager.requestForUpdatingLocation()
mapView.myLocationEnabled = true
locationManager.startUpdatingLocation()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
locationManager.stopUpdatingLocation()
}
// MARK: - AKLocationManagerDelegate
extension DemoViewController: AKLocationManagerDelegate {
func locationManager(manager: AKLocationManager, didGetFirstLocation location: CLLocation) {
mapView.camera = GMSCameraPosition.cameraWithLatitude(location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 12)
mapView.alpha = 1
}
}
Before enable my location with
myLocationEnabled
property fromGMSMapView
class, callrequestForUpdatingLocation()
method.
- iOS 8.0+
- Xcode 7.3+
- Clone or download demo project.
- Add
AKLocationManager
folder to your project.
Demo project require GoogleMaps pod. You need install pod with command
pod install
. OpenDemo.xcworkspace
file and past your api key__API_KEY__
from Google Developers Console.
func requestForUpdatingLocation(requestType: AKLocationManagerRequestType)
Requests permission to use location services.
Parameters:
-
requestType
: Request authorization type for Location Services.
Represents asAKLocationManagerRequestType
:.WhenInUse
: EqualrequestWhenInUseAuthorization()
method. Read more on Apple Developer.Always
: EqualrequestAlwaysAuthorization()
method. Read more on Apple Developer
Default value is
.WhenInUse
.
func startUpdatingLocation()
Starts the generation of updates that report the user’s current location.
func startUpdatingLocationWithRequest(requestType: AKLocationManagerRequestType)
Starts the generation of updates that report the user’s current location with requests permission to use location services.
Parameters:
requestType
: Request authorization type for Location Services. About this property read in "Requesting Authorization for Location Services".
func stopUpdatingLocation()
Stops the generation of location updates.
func requestLocation()
Request the one-time delivery of the user’s current location.
func addObserver(target: AnyObject, forKeyPath keyPath: String)
Registers an observer to receive KVO notifications for the specified key-path relative to the receiver.
Parameters:
target
: The object to register for KVO notifications.keyPath
: The key path, relative to the receiver, of the property to observe. This value must not be nil.
func removeObserver()
Stops a given object from receiving change notifications for the property specified by a given key-path relative to the receiver.
var myLocation: CLLocation? {get}
The most recently retrieved user location. (read-only). The value of this property is nil
if no location data has ever been retrieved.
var updateSpeed: CLLocationSpeed
Specifies the speed in meters per second, when location must be updated not less that updateLocationTimeInterval
minimum property.
Example:
0
: Stand0.3 - 1.4
: Walk1.4 - 4
: Run4 - 12
: Bicycle
The initial value of this property is 0.4
meters per second. Walk.
var updateLocationTimeInterval: AKLocationManagerTimeInterval
Specifies the minimum and maximum update time intervar in secounds. Updating location will be no earlier than minimum time interval value with condition: "current speed more or equal to updateSpeed property and not equal zero". But if the condition is not satisfied, updating location will be no earlier than maximum time interval value.
The initial value of this property is 5.0
seconds for minimum time interval and 300.0
seconds for maximum.
var firstLocationAttempts: Int
Specifies the count of attempts when location manager will try to get first location. One attempt every second. When all attempts is over, location manager will return error .LocationManagerCantDetectFirstLocation
with error protocol method: func locationManager(manager: AKLocationManager, didGetError error: AKLocationManagerError)
.
The initial value of this property is 5
attempts.
weak var delegate: AKLocationManagerDelegate?
The delegate object to receive update events.
func locationManager(manager: AKLocationManager, didGetFirstLocation location: CLLocation)
Tells the delegate that fitst location data is received.
Parameters:
manager
: The location manager object that generated the update event.location
:The most recently retrieved user location.
func locationManager(manager: AKLocationManager, didUpdateLocation location: CLLocation)
Tells the delegate that new location data is available. Default sender of this method on core location manager. After registering observer (ex. Google Maps) sender will be custom location manager.
Parameters:
manager
: The location manager object that generated the update event.location
:The most recently retrieved user location.
locationManager(manager: AKLocationManager, didUpdateLocation location: CLLocation, afterTimeInterval timeInterval: NSTimeInterval)
Default sender of this method on core location manager. After registering observer (ex. Google Maps) sender will be custom location manager. Updating location will be no earlier than minimum or maximum time interval value.
Parameters:
manager
: The location manager object that generated the update event.location
:The most recently retrieved user location.timeInterval
: Time interval value from since last update.
func locationManager(manager: AKLocationManager, didGetError error: AKLocationManagerError)
Tells the delegate that the location manager was unable to retrieve a location value.
Parameters:
manager
: The location manager object that generated the update event.error
: The error object containing the reason the location or heading could not be retrieved.
Represents asAKLocationManagerError
:LocationManagerCantDetectFirstLocation
: Error returns when location manager can't detect first location when all attempts is over.
func locationManager(manager: AKLocationManager, didGetNotification notification: AKLocationManagerNotification)
Tells the delegate that the location manager detect new notification.
Parameters:
manager
: The location manager object that generated the update event.notification
: Current notification.
Represents asAKLocationManagerNotification
enumeration object.LocationManagerNotAuthorized
: Notification returns when you start starts the generation of updates but location manager not authorized.UserAuthorizationDenied
: Notification returns when user denied generation of location updates in device Settings (select Never).UserAuthorizedAlways
: Notification returns when user allowed generation of location updates in device Settings (select Always).UserAuthorizedWhenInUse
: Notification returns when user allowed generation of location updates in device Settings (select While Using the App).AuthorizationDenied
: Notification returns when location manager detect changing authorization status.AuthorizedAlways
: Notification returns when location manager detect changing authorization status.AuthorizedWhenInUse
: Notification returns when location manager detect changing authorization status.AppInBackground
: Notification returns when the app is no longer active and loses focus.AppActive
: Notification returns when the app becomes active.
Please do not forget to ★ this repository to increases its visibility and encourages others to contribute.
Artem Krachulov: www.artemkrachulov.com Mail: [email protected]
Released under the MIT license