ドキュメントやサンプルコードを参考にしているのですが、地図上にユーザーが表示されません。
何が足りないのでしょうか?
ドキュメントを読んで必要っぽいと思ったので記述したこと
- 位置情報の更新
- ユーザーの位置をマップに表示する
- ユーザー追跡モード アニメーション
- mapViewのセンターにユーザーの位置を設定
Swift
1import UIKit 2import MapKit 3import CoreLocation 4 5class ViewController: UIViewController ,MKMapViewDelegate,CLLocationManagerDelegate{ 6 @IBOutlet weak var mapView: MKMapView! 7 let locationManager = CLLocationManager() 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 } 12 13 override func viewWillAppear(_ animated: Bool) { 14 15 16 //CLLocationManagerからイベントを取得 17 locationManager.delegate = self 18 19 //デバイスの位置情報が有効か確認 20 CLLocationManager.locationServicesEnabled() 21 22 //位置情報が無効の場合有効にして! 23 let authorizationStatus = CLLocationManager.authorizationStatus() 24 25 if (authorizationStatus == CLAuthorizationStatus.notDetermined) { 26 locationManager.requestWhenInUseAuthorization() 27 } else if (authorizationStatus == CLAuthorizationStatus.restricted) { 28 locationManager.requestWhenInUseAuthorization() 29 } else if (authorizationStatus == CLAuthorizationStatus.denied) { 30 locationManager.requestWhenInUseAuthorization() 31 32 } else { 33 return 34 } 35 36 //位置情報の更新 37 locationManager.startUpdatingLocation() 38 39 //マップのコード 40 //mapViewのインスタンスを作成 41 let mapView = MKMapView() 42 43 //MKMapViewからイベントを取得 44 mapView.delegate = self 45 46 //ユーザーの位置をマップに表示する 47 mapView.showsUserLocation = true 48 49 //ユーザー追跡モード アニメーション 50 mapView.userTrackingMode = MKUserTrackingMode.followWithHeading 51 52 //mapViewのセンターにユーザーの位置を設定 53 mapView.setCenter(mapView.userLocation.coordinate, animated: true) 54 55 //縮尺を設定 56 var region:MKCoordinateRegion = mapView.region 57 region.center = mapView.userLocation.coordinate 58 region.span.latitudeDelta = 0.02 59 region.span.longitudeDelta = 0.02 60 mapView.setRegion(region, animated: true) 61 62 //アノテーションを設定 63 let annotation = MKPointAnnotation() 64 annotation.coordinate = CLLocationCoordinate2DMake(35.68154, 139.752498) 65 66 //表示タイプを航空写真と地図のハイブリッドに設定 67 mapView.mapType = MKMapType.hybrid 68 69 70 } 71 72 override func didReceiveMemoryWarning() { 73 super.didReceiveMemoryWarning() 74 // Dispose of any resources that can be recreated. 75 } 76 77 78} 79 80
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。