######リファレンスの「showsMapTypeControl」をmapViewに使いたい。
地図の切り替え機能を実装したいので、この質問をしています。
Bool型なので下記の適用方法を試しましたが違うようです。
「showsMapTypeControlの使い方」と
を「皆さんはどのような方法でリファレンスにあるコードの使い方を調べているのか?」
あわせて教えていただきたいです。よろしくお願いします。
######当該のコード
map.showsMapTypeControl = true
######コード全文
// // MemoViewController.swift // Wood volume // // Created by MBP13 on 2019/05/09. // Copyright © 2019 Tomoyuki Ashikari. All rights reserved. // import UIKit import MapKit import CoreLocation class MemoViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate,CLLocationManagerDelegate { @IBOutlet weak var saveButton: UIBarButtonItem! var locationManager:CLLocationManager! var memo: String?//saveボタン用 @IBOutlet weak var memoTextField: UITextField! @IBOutlet weak var map: MKMapView! @IBOutlet weak var latiudeLabel: UILabel! @IBOutlet weak var longitudeLabel: UILabel! @IBOutlet weak var satelliteLabel: UILabel! @IBOutlet weak var dopLabel: UILabel! @IBOutlet weak var measureButton: UIButton! var cnt : Int = 0 //表示領域を指定:はじめの1回だけ現在地をセンターにするためif cntを設置 @IBAction func measureButton(_ sender: Any) { print("開始") } override func viewDidLoad() { super.viewDidLoad() locationManager = CLLocationManager() locationManager.desiredAccuracy = kCLLocationAccuracyBest //最高精度 // kCLLocationAccuracyBestForNavigation ナビゲーションに最適な値 // kCLLocationAccuracyBest 最高精度(iOS,macOSのデフォルト値) // kCLLocationAccuracyNearestTenMeters 10m // kCLLocationAccuracyHundredMeters 100m(watchOSのデフォルト値) // kCLLocationAccuracyKilometer 1Km // kCLLocationAccuracyThreeKilometers 3Km locationManager.activityType = .fitness//歩行者other その他(デフォルト値) // automotiveNavigation 自動車ナビゲーション用 // fitness 歩行者 // otherNavigation その他のナビゲーションケース(ボート、電車、飛行機) locationManager.requestAlwaysAuthorization() locationManager.delegate = self // CLLocationManagerDelegat locationManager.startUpdatingLocation()//観測開始 // func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // // let location : CLLocation = locations[0] // let latlng = location.coordinate // print("緯度:(latlng.latitude) 経/度:(latlng.longitude) 取得時刻:(location.timestamp.description)") // print("関数の中") // // for location in locations { // // print("緯度:(location.coordinate.latitude) 経/度:(location.coordinate.longitude) 取得時刻:(location.timestamp.description)") // // } // // } //文字が入力されるまでsaveButtonを使わない //self.saveButton.isEnabled = false //タイトルを変える self.navigationItem.title = "Edit Memo" //値を受け取る //Optional Binding を使ってあげて、 if let memo = self.memo で memo に self.memo をセットしつつ、もし値がセットされていたら、 textField に memo を渡してあげれば OK でしょう。 if let memo = self.memo{ self.memoTextField.text = memo } self.updateSaveButtonState() // Do any additional setup after loading the view, typically from a nib. // 地図の設定〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 map.mapType = MKMapType.satelliteFlyover map.showsMapTypeControl = true // 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜地図の設定 } private func updateSaveButtonState(){ let memo = self.memoTextField.text ?? "" //memoがnilだったら空文字 self.saveButton.isEnabled = !memo.isEmpty //saveButtonnが有効なのはsaveButtonが空でないとき } //textField に入力された値を検出 //textField を Control + クリックすると Editing Changed //という項目があるので、これをこのあたりにドラッグしてあげてメソッドを作っていきます。 //extField の中身が変わった時の処理をこちらに書くことができます。 @IBAction func memoTextFieldChanged(_ sender: Any) { updateSaveButtonState() } //cancel の場合は特に渡すデータもないので、単にこのモーダル(画面)を消してあげれば OK ですね。 //どうするかというと dismiss という命令が使えるので self.dismiss( … のように書いてあげましょう @IBAction func cancel(_ sender: Any) { // self.dismiss(animated: true, completion: nil) /* 0:02 さて Cancel には今 dismiss という命令が設定されていますが、これはモーダルウィンドウを閉じるための命令です。 0:09 Cell をタップして編集する時はモーダルではなくて、こちらの Show した segue を逆に辿っていく必要があります。 0:16 したがって Cancel した時の処理で条件分岐をしてあげましょう。 0:22 どうするかというと、実はモーダルで表示した時は self.presentingViewController に今の ViewController を表示している ViewController がセットされます。 0:35 モーダルで表示した場合は、こちらの NavigationController を介して表示するので、この presentingViewController が UINavigationController だったらモーダルの処理なので、今までどおり dismiss() を使ってあげれば OK です。 0:56 そうではなかったら編集の時の処理なので、その場合は Show した segue を逆に戻るには popViewController という命令を使ってあげてください。 1:08 アニメーションは true で OK かと思います。 */ if self.presentingViewController is UINavigationController { self.dismiss(animated: true, completion: nil) } else { self.navigationController?.popViewController(animated: true) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //saveボタンが押されたときの処理だったらsenderに入ってくるのでguardでチェック //ボタンアイテムでキャストする //saveボタンと同じか比較する //そうじゃなかったらreturnでとめる //これでセグエが巻き戻る時に値がセットされる override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard let button = sender as? UIBarButtonItem, button === self.saveButton else { return } self.memo = self.memoTextField.text ?? "" } } //位置情報の許可=========================================== extension MemoViewController { func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { switch status { case .notDetermined: print("ユーザーはこのアプリケーションに関してまだ選択を行っていません") locationManager.requestAlwaysAuthorization() // 常に許可 break case .denied: print("ローケーションサービスの設定が「無効」になっています (ユーザーによって、明示的に拒否されています)") // アラート生成 let alert : UIAlertController = UIAlertController(title: "測定を続けるには", message: "設定 > プライバシー > 位置情報サービス で、位置情報サービスの利用を許可して下さい。", preferredStyle: .alert) // OKボタンを追加 let okAction = UIAlertAction(title:"OK", style: .default,handler: nil) alert.addAction(okAction) // 表示 present(alert,animated: true,completion: nil) break case .restricted: print("このアプリケーションは位置情報サービスを使用できません(ユーザによって拒否されたわけではありません)") // 「このアプリは、位置情報を取得できないために、正常に動作できません」を表示する break case .authorizedAlways: print("常時、位置情報の取得が許可されています。") // 位置情報取得の開始処理 break case .authorizedWhenInUse: print("起動時のみ、位置情報の取得が許可されています。") // 位置情報取得の開始処理 locationManager.requestAlwaysAuthorization() // 常に許可 break @unknown default: break } } //===========================================位置情報の許可 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {//GPSが更新すると呼ばれる let location : CLLocation = locations[0] let currentlocation = location.coordinate //表示領域を指定:はじめの1回だけ現在地をセンターにするためif cntを設置 if cnt == 0{ let reg : MKCoordinateRegion = MKCoordinateRegion(center: currentlocation,latitudinalMeters: 100,longitudinalMeters: 100) //アノテーションを生成し、表示 // let ann : MKPointAnnotation = MKPointAnnotation() map.region = reg //ann.title = "現在地" //map.addAnnotation(ann) //仮想カメラの位置 let viewPoint :CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude-0.005, location.coordinate.longitude) //ビルを見せる map.showsBuildings = true //拡大縮小 map.isPitchEnabled = true //3D用の仮想カメラオブジェクトを500m上空にセット let cameraVR : MKMapCamera = MKMapCamera(lookingAtCenter: currentlocation, fromEyeCoordinate: viewPoint, eyeAltitude: 100) map.camera = cameraVR cnt = 1 } print("緯度:(location.coordinate.latitude) 経度:(location.coordinate.longitude) 取得時刻:(location.timestamp.description)") print("horizontalAccuracyは(location.horizontalAccuracy)") print("verticalAccuracyは(location.verticalAccuracy)") latiudeLabel.text = String("緯度:(location.coordinate.latitude)" ) longitudeLabel.text = String("経度:(location.coordinate.longitude)") satelliteLabel.text = String("horizontalAccuracyは(location.horizontalAccuracy)") dopLabel.text = String("verticalAccuracyは(location.verticalAccuracy)") } }
回答1件
あなたの回答
tips
プレビュー