前提・実現したいこと
viewDidLoad内でlocationManagerメソッドを呼び込みたい---
ここに質問の内容を詳しく書いてください。
swiftで地図アプリを作っています
位置情報の取得まではできており、それ以降指定した地域を表示したいのですが、どのようにしてviewDidLoad内に呼び込めば良いかわかりません。
発生している問題・エラーメッセージ
呼び込み方がわからない
該当のソースコード
↓のコードでlocationManagerのメソッドを作って
Swift5
1 //現在地の取得に成功した場合の処理 2 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 3 print("現在地の取得に成功しました") 4 //配列から現在座標を取得(配列lovationsの中から最新のものを取得する) 5 let myLocation = locations.last! as CLLocation 6 //pinに表示するためにはCLLocationCoordinate2Dに変換してあげる必要がある 7 let currentLocation = myLocation.coordinate 8 //ピンの生成と配置 9 let pin = MKPointAnnotation() 10 pin.coordinate = currentLocation 11 pin.title = "現在地" 12 self.myMapView.addAnnotation(pin) 13 //アプリ起動時の表示領域の設定→最初のページで書かれた位置情報の場所に飛びたい 14 15 var mySpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) 16 var myRegion = MKCoordinateRegion(center: currentLocation, span: mySpan) 17 18 if ViewController().regionNumber == 0 { 19 myMapView.region = myRegion 20 } else if ViewController().regionNumber == 1 { 21 //ここで表示する領域を場所ごとに変える 22 mySpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) 23 myRegion = MKCoordinateRegion(center: currentLocation, span: mySpan) 24 25 } 26 }
下のviewDidLoadに呼び込みたいのですが呼び込み方がわからないです
Swift5
1 override func viewDidLoad() { 2 super.viewDidLoad() 3 4 //MapViewを生成し、表示する 5 myMapView.frame = self.view.frame 6 self.view.addSubview(myMapView) 7 8 //長押しを探知する機能を追加 9 //ジェスチャーを生成 10 let longPressGesture = UILongPressGestureRecognizer() 11 //ボタンを押した時の処理 12 longPressGesture.addTarget(self, action: #selector(longPressed(sender:))) 13 myMapView.addGestureRecognizer(longPressGesture) 14 15 //ここからが現在地取得の処理 16 myLocationManager.delegate = self 17 18 self.locationManager.requestWhenInUseAuthorization() 19 20 //セキュリティ認証のステータスを取得 21 let status = CLLocationManager.authorizationStatus() 22 if status == CLAuthorizationStatus.notDetermined { 23 //まだ認証が得られてない場合は、認証ダイアログを表示 24 myLocationManager.requestAlwaysAuthorization() 25 } 26 27 //現在地取得の開始 28 myLocationManager.startUpdatingLocation() 29 print("現在地取得なう") 30 } 31
補足情報(FW/ツールのバージョンなど)
Swift5の最新版です
あなたの回答
tips
プレビュー