This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandardLocation.swift
68 lines (56 loc) · 2.22 KB
/
StandardLocation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// StandardLocation.swift
// Pogger
//
// Created by Taiju Aoki on 2017/01/04.
// Copyright © 2017年 Taiju Aoki. All rights reserved.
//
import CoreLocation
protocol StandardLocationDelegate: class {
func standardLocation(_ standardLocation: StandardLocation, manager: CLLocationManager, didUpdateLocation location: CLLocation)
func standardLocation(_ standardLocation: StandardLocation, manager: CLLocationManager, didFailWithError error: Error)
}
class StandardLocation: NSObject, CLLocationManagerDelegate {
private var manager = CLLocationManager()
static let sharedInstance = StandardLocation()
weak var delegate: StandardLocationDelegate?
override private init() {
super.init()
manager.delegate = self
manager.requestAlwaysAuthorization()
manager.allowsBackgroundLocationUpdates = true
manager.pausesLocationUpdatesAutomatically = false // これを入れないと停止した場合に15分ぐらいで勝手に止まる
//位置情報精度
let userDefaults = UserDefaults.standard
if let lq = LocationQuality(rawValue: userDefaults.integer(forKey: Prefix.keyLocateQuality)) {
setAccuracy(lq)
} else {
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
}
}
func request() {
manager.requestLocation()
}
func start() {
manager.startUpdatingLocation()
}
func stop() {
manager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
delegate?.standardLocation(self, manager: manager, didUpdateLocation: manager.location!)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
delegate?.standardLocation(self, manager: manager, didFailWithError: error)
}
func setAccuracy(_ locateQuality: LocationQuality) {
switch locateQuality {
case .high:
manager.desiredAccuracy = kCLLocationAccuracyBest
case .normal:
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
case .low:
manager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
}
}
}