### 現在地の情報を取得してUILabelに緯度と経度を表示したいのですがエラーが出てしまいうまくいきません。
どうぞよろしくお願いいたします。
Swift
1import UIKit 2import MapKit 3import CoreLocation 4 5class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate { 6 7 @IBOutlet var mapView: MKMapView! 8 @IBOutlet var outLatitude: UILabel! 9 @IBOutlet var outLongitude: UILabel! 10 var locationManager = CLLocationManager() 11 var loctionManger: CLLocationManager! 12 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 let myLocationManager = CLLocationManager() 18 //使用中のみ位置情報の取得を許可 19 myLocationManager.requestWhenInUseAuthorization() 20 mapView.delegate = self 21 myLocationManager.delegate = self 22 setupLocationManager() 23 24 } 25 26 func setupLocationManager() { 27 28 locationManager = CLLocationManager() 29 30//以下の"guard let ..."にエラーが表示されます。 31 guard let locationManager = locationManager else { return } 32 locationManager.requestWhenInUseAuthorization() 33 34 locationManager.requestWhenInUseAuthorization() 35 36 let status = CLLocationManager.authorizationStatus() 37 if status == .authorizedWhenInUse { 38 locationManager.distanceFilter = 10 39 locationManager.startUpdatingLocation() 40 } 41 42 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 43 let location = locations.first 44 let latitude = location?.coordinate.latitude 45 let longitude = location?.coordinate.longitude 46 47 } 48 49 } 50 51 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 52 53 var statusStr = "" 54 55 switch (status) { 56 case .notDetermined: 57 statusStr = "NotDetermined" 58 case .restricted: 59 statusStr = "Restricted" 60 case .denied: 61 statusStr = "Denied" 62 case .authorizedAlways: 63 statusStr = "AuthorizedAlways" 64 case .authorizedWhenInUse: 65 statusStr = "AuthorizedWhenInUse" 66 @unknown default: 67 break 68 } 69 70 } 71 72 @IBAction func getInfo(_ sender: Any) { 73 74 locationManager.startUpdatingLocation() 75 76 77 func locationManager(manager: CLLocationManager!,didUpdateLocations locations: [AnyObject]!){ 78 outLatitude.textAlignment = NSTextAlignment.center 79 outLongitude.textAlignment = NSTextAlignment.center 80 81 self.view.addSubview(outLatitude) 82 self.view.addSubview(outLongitude) 83 } 84 85 func locationManager(manager: CLLocationManager!,didFailWithError error: NSError!){ 86 print("error") 87 } 88 } 89 90} 91
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/23 12:03
2019/07/23 12:16