13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CLLocationManagerのシングルトンクラス

Last updated at Posted at 2014-12-23

クラスの定義

LocationManager.swift
import CoreLocation
    
let LMLocationUpdateNotification : NSString = "LMLocationUpdateNotification"
    
let LMLocationInfoKey : NSString = "LMLocationInfoKey"
    
class LocationManager: NSObject, CLLocationManagerDelegate {
    
    private var locationManager_: CLLocationManager
    private var currentLocation: CLLocation!
    
    struct Singleton {
        static let sharedInstance = LocationManager()
    }

    class var sharedInstance: LocationManager {
        return Singleton.sharedInstance
    }

    override init() {
        locationManager_ = CLLocationManager()
        locationManager_.desiredAccuracy = kCLLocationAccuracyBest
        locationManager_.distanceFilter = 100 // meters
        super.init()
        locationManager_.delegate = self
        // iOS8用のメソッドがあるかチェック        
        if (self.locationManager_.respondsToSelector("requestWhenInUseAuthorization")) {
            self.locationManager_.requestWhenInUseAuthorization()
        }
    }


    func startUpdatingLocation()
    {
        println("Starting location updates")
        self.locationManager_.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("Location service failed with error: \(error.localizedDescription)")
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var location = locations.last as CLLocation
        println("Location = \(location)")

        self.currentLocation = location

        var userInfo = [ LMLocationInfoKey : location]
        var center = NSNotificationCenter.defaultCenter()
        center.postNotificationName(LMLocationUpdateNotification, object:self, userInfo: userInfo)
        self.locationManager_.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if (status == .NotDetermined) {
            if (self.locationManager_.respondsToSelector("requestWhenInUseAuthorization")) {
                self.locationManager_.requestWhenInUseAuthorization()
            }
        }
    }
}

How to use

LocationManager.sharedInstance.startUpdatingLocation() で現在地探す。
現在地が見つかるとLMLocationUpdateNotificationが通知センターに送られるので、観測する側はその通知名を登録しておく。
userInfo辞書にはLMLocationKeyにlocationが入っている。

以下、例。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var center = NSNotificationCenter.defaultCenter() as NSNotificationCenter

        LocationManager.sharedInstance.startUpdatingLocation()
        center.addObserver(self, selector: "foundLocation:", name: LMLocationUpdateNotification, object: nil)
}

func foundLocation(notif:NSNotification) {
    println("foundLocation called")

    let info = notif.userInfo as NSDictionary!
    var location = info[LMLocationInfoKey] as CLLocation

    var coordinate = location.coordinate
    // ...適当に位置情報を処理
}
13
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?