前提・実現したいこと
Xcode10で現在地をシミュレータで表示させたいです。
現在地は「Product」 -> 「Scheme」 -> 「Edit Scheme」 -> 「Options」 -> 「Allow Location Simulation」 にチェックを入れて、Default Locationを「Tokyo, Japan」に設定しました。
info.plistは以下のように設定しました。
発生している問題・エラーメッセージ
エラーは表示されないのですが、シミュレータを起動させると現在地が出てきません。
該当のソースコード
Swift
1import UIKit 2import MapKit 3import CoreLocation 4 5class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 6 7 let myMapView = MKMapView() 8 let myLocationManager = CLLocationManager() 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 myMapView.frame = self.view.frame 14 self.view.addSubview(myMapView) 15 16 //ジェスチャーの生成 17 let longPressGesture = UILongPressGestureRecognizer() 18 //ボタンを押した時の処理 19 longPressGesture.addTarget(self, action: #selector(longPressed(sender:))) 20 myMapView.addGestureRecognizer(longPressGesture) 21 22 myMapView.delegate = self 23 24 myLocationManager.delegate = self 25 26 //セキリュティ認証のステータスを取得 27 let status = CLLocationManager.authorizationStatus() 28 //まだ承認が得られていない場合 29 if status == CLAuthorizationStatus.notDetermined { 30 //認証ダイアログを表示 31 myLocationManager.requestAlwaysAuthorization() 32 } 33 } 34 35 //長押しした時にピンを置く処理 36 @objc func longPressed(sender: UILongPressGestureRecognizer) { 37 if sender.state != UIGestureRecognizer.State.began { 38 return 39 } 40 //senderから長押しした地図上の座標を取得 41 let tappedLocation = sender.location(in: myMapView) //View上の位置を取得 42 let tappedPont = myMapView.convert(tappedLocation, toCoordinateFrom: myMapView) 43 44 //注釈の生成 45 let annotaion = MKPointAnnotation() 46 //場所を指定 47 annotaion.coordinate = tappedPont 48 //タイトルを指定 49 annotaion.title = "タイトル" 50 //サブタイトルを指定 51 annotaion.subtitle = "サブタイトル" 52 //注釈をMapViewに追加することで注釈の情報を持ったピンを生成 53 self.myMapView.addAnnotation(annotaion) 54 } 55 56 //ピンの設定 57 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 58 //ピンの再利用 59 var annotationView = myMapView.dequeueReusableAnnotationView(withIdentifier: "annotation") as? MKPinAnnotationView 60 if annotationView == nil { 61 annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotation") 62 } 63 64 //落下アニメーションを有効にする 65 annotationView?.animatesDrop = true 66 67 //ピンがタップされた時に出るビューを表示可能にする 68 annotationView?.canShowCallout = true 69 70 //ドラッグ可能にする 71 annotationView?.isDraggable = true 72 73 return annotationView 74 } 75 76 //GPSから値を取得した際に呼び出されるメソッド 77 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 78 79 //配列から現在座標を取得(配列locationsの中から最新のものを取得する) 80 let myLocation = locations.last! as CLLocation 81 //Pinに表示するためにCLLocationCoordinate2Dに変換 82 let currentLocation = myLocation.coordinate 83 84 //アプリ起動時の表示領域設定 85 //delta数字を大きくして表示領域を広げる 86 let mySpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) 87 let myRegion = MKCoordinateRegion(center: currentLocation, span: mySpan) 88 //表示領域のアニメーション付きで適用 89 myMapView.setRegion(myRegion, animated: true) 90 } 91}
試したこと
シミュレータはiPhone8,X,XRで試しました。
デフォルトの位置情報を設定し直しても無理でした。
補足情報(FW/ツールのバージョンなど)
Xcode10.1
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/05 10:21
2018/12/06 00:06