Skip to content

Latest commit

 

History

History
279 lines (193 loc) · 9.66 KB

README.md

File metadata and controls

279 lines (193 loc) · 9.66 KB

AKLocationManager

AKLocationManager is class which can better controll your location.

Features

  • 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.

Usage

Standalone

var locationManager: AKLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = AKLocationManager()
    locationManager.startUpdatingLocationWithRequest()
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    locationManager.stopUpdatingLocation()
}

With google maps

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 from GMSMapView class, call requestForUpdatingLocation() method.


Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

  1. Clone or download demo project.
  2. Add AKLocationManager folder to your project.

Demo project require GoogleMaps pod. You need install pod with command pod install. Open Demo.xcworkspace file and past your api key __API_KEY__from Google Developers Console.

Requesting Authorization for Location Services

func requestForUpdatingLocation(requestType: AKLocationManagerRequestType)

Requests permission to use location services.

Parameters:

  • requestType : Request authorization type for Location Services.
    Represents as AKLocationManagerRequestType:

    • .WhenInUse : Equal requestWhenInUseAuthorization() method. Read more on Apple Developer
    • .Always : Equal requestAlwaysAuthorization() method. Read more on Apple Developer

    Default value is .WhenInUse.

Initiating Standard Location Updates

 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.

Observerving custom location manager

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.

Getting Recently Retrieved Data

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.

Configurate

var updateSpeed: CLLocationSpeed

Specifies the speed in meters per second, when location must be updated not less that updateLocationTimeInterval minimum property.

Example:

  • 0 : Stand
  • 0.3 - 1.4 : Walk
  • 1.4 - 4 : Run
  • 4 - 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.

Accessing the Delegate

weak var delegate: AKLocationManagerDelegate?

The delegate object to receive update events.

AKLocationManagerDelegate

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 as AKLocationManagerError:
    • 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 as AKLocationManagerNotification 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.

Author

Artem Krachulov: www.artemkrachulov.com Mail: [email protected]

License

Released under the MIT license