@@ -13,7 +13,7 @@ class TolietAnnotation : MKPointAnnotation {}
1313class BuildingAnnotation : MKPointAnnotation { }
1414class WebcamAnnotation : MKPointAnnotation { }
1515
16- class MapViewController : UIViewController , MKMapViewDelegate {
16+ class MapViewController : UIViewController , MKMapViewDelegate , CLLocationManagerDelegate {
1717 @IBOutlet weak var mapView : MKMapView !
1818 var buildings = [ String] ( )
1919 var toilets = [ String] ( )
@@ -29,8 +29,7 @@ class MapViewController: UIViewController,MKMapViewDelegate {
2929 override func viewDidLoad( ) {
3030 super. viewDidLoad ( )
3131
32- lm. requestWhenInUseAuthorization ( )
33- lm. startUpdatingLocation ( )
32+ lm. delegate = self
3433 buildings = loadCSV ( name: " buildings_locations " )
3534 toilets = loadCSV ( name: " toilets_locations " )
3635 webcams = loadCSV ( name: " webcams_locations " )
@@ -76,13 +75,6 @@ class MapViewController: UIViewController,MKMapViewDelegate {
7675 }
7776 }
7877
79- override func viewDidAppear( _ animated: Bool ) {
80- if needResetRegion {
81- resetRegion ( )
82- needResetRegion = false
83- }
84- }
85-
8678 @IBAction func didTapCurrentButton( _ sender: Any ) {
8779 resetRegion ( )
8880 }
@@ -173,6 +165,24 @@ class MapViewController: UIViewController,MKMapViewDelegate {
173165 }
174166 }
175167
168+ // 位置情報取得の許可状況が変わると呼ばれる
169+ func locationManager( _ manager: CLLocationManager , didChangeAuthorization status: CLAuthorizationStatus ) {
170+ switch status {
171+ case . authorizedWhenInUse:
172+ lm. startUpdatingLocation ( )
173+ default :
174+ lm. requestWhenInUseAuthorization ( )
175+ }
176+ }
177+
178+ // 位置情報が変更するたびに呼ばれる
179+ func locationManager( _ manager: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
180+ if needResetRegion {
181+ resetRegion ( )
182+ needResetRegion = false
183+ }
184+ }
185+
176186 private func loadCSV( name: String ) -> [ String ] {
177187 let path = Bundle . main. path ( forResource: name, ofType: " csv " ) !
178188 let csvString = try ! String ( contentsOfFile: path, encoding: String . Encoding. utf8)
0 commit comments