前提・実現したいこと
ここに質問の内容を詳しく書いてください。
swiftで地図アプリを作成しています。
アプリ読み込み時に自分のいる位置にアノテーションを表示し、画面を長押しするとさらにアノテーションを表示すしています。 それを配列に入れて、削除のボタンを押すと一つずつアノテーションを削除していきたいのです。
発生している問題・エラーメッセージ
画面長押しで表示するアノテーションは削除できますが
画面読み込み時に表示するアノテーションは削除できません。
配列に入れているのですが、、、
該当のソースコード
import UIKit
import Firebase
import MapKit
import CoreLocation
class SecondViewController: UIViewController , MKMapViewDelegate , CLLocationManagerDelegate,UIGestureRecognizerDelegate,UITextFieldDelegate ,
UIImagePickerControllerDelegate,UINavigationControllerDelegate{
var myIdo : Double = 0.000000 var myKeido : Double = 0.0000 var annotationlist = Array<MKPointAnnotation>() // アノテーションの配列
//ここからUIのための宣言開始
@IBOutlet weak var message_Label: UILabel!
@IBOutlet weak var myMap: MKMapView!
@IBAction func Jihanki_Button(_ sender: Any) { message_Label.text = "efgh" } @IBAction func Kyukei_Button(_ sender: Any) { } @IBAction func keshiki_Button(_ sender: Any) { } @IBAction func syokuji_Button(_ sender: Any) { } @IBAction func konbini_Button(_ sender: Any) { } @IBAction func jitensyaStore_Button(_ sender: Any) { } @IBAction func mapLomgPress(_ sender: UILongPressGestureRecognizer) { guard sender.state == UIGestureRecognizer.State.ended else { // 長押しの終了でのみ実行する return //長押しの開始と最中はキャンセルする } let pressPoint = sender.location(in: myMap) // 長押しの座標を取り出す let pressCoordinate = myMap.convert(pressPoint, toCoordinateFrom: myMap) // マップの座標に変換する let userDefaults = UserDefaults.standard let Ido = pressCoordinate.latitude //緯度を取り出す let Keido = pressCoordinate.longitude userDefaults.set(Ido,forKey : "緯度") //ユーザデフォルトに保存して次の画面で使う userDefaults.set(Keido,forKey : "経度") // アノテーションを作る let annotation = MKPointAnnotation() annotation.coordinate = pressCoordinate annotation.title = "ここに追加" annotationlist.append(annotation) // アノテーションを配列に追加する myMap.addAnnotation(annotation)// アノテーションを地図に表示する } // 虫ピンを使う func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let pinView = MKPinAnnotationView()// ドラッグ可能なピンを作る pinView.animatesDrop = true // 落ちてくる pinView.isDraggable = true // ドラッグ可能 pinView.pinTintColor = UIColor.blue // 色 pinView.canShowCallout = true // タップでタイトル表示 pinView.rightCalloutAccessoryView = UIButton(type: UIButton.ButtonType.detailDisclosure) return pinView } //ボタンを押された時 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { //画面遷移をコードでするのはしたの2行 let viewController = self.storyboard!.instantiateViewController(withIdentifier: "syousaiStoryboard") self.present(viewController, animated: true, completion: nil) } //変数の宣言等 var locationManager = CLLocationManager() //位置情報開始準備 override func viewDidLoad() { super.viewDidLoad() locationManager.requestWhenInUseAuthorization() // アプリ利用中の位置情報の利用許可を得る locationManager.delegate = self// ロケーションマネージャのdelegeteになる setupLocationService()// ロケーション機能の設定 myMap.delegate = self //ピン表示用デリゲートはこの1行だけ startMapMyLoc() }
// ロケーション機能の設定
func setupLocationService() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest // ロケーションの精度を設定する(ベスト)
locationManager.distanceFilter = 0.01 // ロケーションの精度を設定する(ベスト)
}
//
// // 位置情報利用許可のステータスが変わった
// func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
// switch status {
// case .authorizedAlways, .authorizedWhenInUse :
// // ロケーションの更新を開始する
// locationManager.startUpdatingLocation()
// let msg = "位置情報の利用が許可されている。"
// message_Label.text = msg
// case .notDetermined:
// // ロケーションの更新を停止する
// locationManager.stopUpdatingLocation()
// disabledLocationLabel()
// default:
// // ロケーションの更新を停止する
// locationManager.stopUpdatingLocation()
// disabledLocationLabel()
// }
// }
//
//
// 位置を移動した
// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// // locationsの最後の値を取り出す
// let locationData = locations.last
// // 緯度
// if var ido = locationData?.coordinate.latitude {
// ido = round(ido1000000)/1000000
// myIdo = Double(ido)
// }
// // 経度
// if var keido = locationData?.coordinate.longitude {
// keido = round(keido1000000)/1000000
// myKeido = Double(keido)
// }
// // 標高
// if var hyoukou = locationData?.altitude {
// hyoukou = round(hyoukou*100)/100
//
// }
// }
@IBAction func setCenter(_ sender: Any) { startMapMyLoc() } @IBAction func removePin(_ sender: Any) { // 最後に追加したピンを削除する if annotationlist.count>0{ let lastPin = annotationlist.removeLast() myMap.removeAnnotation(lastPin) } } //位置情報が使えなかった場合にラベルを変更 func disabledLocationLabel() { let msg = "位置情報の利用が許可されてない。" message_Label.text = msg } //読み込み時に自分の位置をアップに表示するため func startMapMyLoc() { let center = myMap.userLocation.coordinate // 中心座標 let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02) // 表示範囲 let region = MKCoordinateRegion(center: center, span: span)// 地図の表示領域を決める myMap.setRegion(region, animated:true) // アノテーションを作る let pressCoordinate = myMap.userLocation.coordinate let annotation = MKPointAnnotation() annotation.coordinate = pressCoordinate annotation.title = "ここに追加" annotationlist.append(annotation) // アノテーションを配列に追加する myMap.addAnnotation(annotation)// アノテーションを地図に表示する print(annotation) }
}
試したこと
同じ配列に入れているつもりなのですが、、、
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー