前提・実現したいこと
Swift初学者です。
Swiftを使って、位置情報からピンを立てて線を引くことで軌跡アプリを作りたいと思っています。
PAUSEボタンを押すと、manager.stopUpdatingLocation()が働いて、位置情報を得ることを止めようとしたところエラーが発生しました。
Swiftを学び始めたばかりで初歩的な質問かもしれませんが教えていただきたいです。
発生している問題・エラーメッセージ
エラーメッセージ Use of unresolved identifier 'locationManager'
該当のソースコード
swift
1ソースコード guard let manager = locationManager else { return } //エラー 2 manager.stopUpdatingLocation() 3 manager.delegate = nil 4
試したこと
紐づけがされているかを確認しました。
追記
locationManager=nilを削除しました。
return managerを追加しました。
補足情報(FW/ツールのバージョンなど)
Swift 3.0.2
Xcode 8.2.1
コード全容を記述します。
swift
1 2import UIKit 3import MapKit 4 5class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 6 7 @IBOutlet weak var testMapView: MKMapView! 8 @IBOutlet weak var START: UIButton! 9 @IBOutlet weak var PAUSE: UIButton! 10 11 @IBAction func startbtntapped(_ sender: UIButton) { 12 13 // Startボタンを無効化 14 START.isEnabled = false 15 // PAUSEボタンを有効化 16 PAUSE.isEnabled = true 17 18 var testManager:CLLocationManager = CLLocationManager() 19 20 //アノテーション 21 var annotation:MKPointAnnotation! 22 23 24 25 //最初からあるメソッド 26 func viewDidLoad() { 27 super.viewDidLoad() 28 29 30 31 //デリゲート先に自分を設定する。 32 testManager.delegate = self 33 34 //位置情報の取得を開始する。 35 testManager.startUpdatingLocation() 36 37 //位置情報の利用許可を変更する画面をポップアップ表示する。 38 testManager.requestWhenInUseAuthorization() 39 40 print("testMapView:", testMapView) 41 //デリゲート先を自分に設定する。 42 testMapView.delegate = self 43 44 } 45 46 47 48 //位置情報取得時の呼び出しメソッド 49 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 50 51 for location in locations { 52 53 //中心座標 54 let center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 55 56 //表示範囲 57 let span = MKCoordinateSpanMake(0.01, 0.01) 58 59 //中心座標と表示範囲をマップに登録する。 60 let region = MKCoordinateRegionMake(center, span) 61 testMapView.setRegion(region, animated:true) 62 63 if(annotation == nil) { 64 //初回はマップにピンを格納する。 65 annotation = MKPointAnnotation() 66 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 67 testMapView.addAnnotation(annotation) 68 } else { 69 //2回目以降は移動前と後の座標間に直線を引く。 70 71 //始点と終点の座標 72 var lineLocation:[CLLocationCoordinate2D] = [CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude), 73 CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)] 74 75 //2点間に直線を描画する。 76 let line = MKPolyline(coordinates: &lineLocation, count: 2) 77 testMapView.add(line) 78 79 //ピンの位置を更新する。 80 annotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 81 } 82 } 83 } 84 85 86 87 //描画メソッド実行時の呼び出しメソッド 88 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 89 let testRender = MKPolylineRenderer(overlay: overlay) 90 91 //直線の幅を設定する。 92 testRender.lineWidth = 3 93 94 //直線の色を設定する。 95 testRender.strokeColor = UIColor.red 96 97 return testRender 98 99 } 100 } 101 102 @IBAction func pausebtntapped(_ sender: UIButton) { 103 104 //Startボタンを有効化 105 START.isEnabled = true 106 //Pauseボタンを無効化 107 PAUSE.isEnabled = false 108 109 guard let manager = locationManager else { return } //ここでエラー 110 manager.stopUpdatingLocation() 111 manager.delegate = nil 112 113 114 // untracking user location 115 testMapView.userTrackingMode = MKUserTrackingMode.none 116 testMapView.showsUserLocation = false 117 testMapView.removeAnnotations(testMapView.annotations) 118 119 return manager 120 121 } 122 123} 124 125
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/19 18:58
2018/11/20 01:08
2018/11/20 04:18