質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2084閲覧

UILabelに現在地の緯度と経度を表示したいです。

SoichiSugimo

総合スコア13

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/07/23 11:30

### 現在地の情報を取得して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

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Swift

1guard let locationManager = locationManager else { return }

の一文を削除してください。

guard let はオプショナル型(?, !が型の後ろにつく型。nilを許容する。)に対してのみ使われます。
今回,

Swift

1var locationManager = CLLocationManager()

と宣言されているので,オプショナル型ではありません。

投稿2019/07/23 11:58

fathy

総合スコア254

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

SoichiSugimo

2019/07/23 12:03

返信ありがとうございます。 早速、ご指摘の箇所を削除して実行したところ、エラーは無くなりました。 ありがとうございます! しかし、他の問題があるようで、UILabelに数値(緯度と経度)は表示されないのです。。。 何かご指導いただけないでしょうか? どうぞ、よろしくお願いいたします。
fathy

2019/07/23 12:16

func 〜(){ } の中にあるfunc 〜(){ } は全て1つ外に出してください。 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.first let latitude = location?.coordinate.latitude let longitude = location?.coordinate.longitude outLatitude.text = String(longitude!) outLongitude.text = String(longitude!) } とすれば表示されるはずです。 UIlabel 表示とかで検索すればすぐ出てきます。 検索をしてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問