LoginSignup
6
5

More than 5 years have passed since last update.

MKMapViewに現在地を表示する

Posted at

MapKitとCoreLocationライブラリをリンクして、StoryboardにMapViewを貼り付けて、MapViewの「User Location」にチェックを入れて、という手順までは、まあ素直に進むと思います。

そのまま起動すると以下のようなエラーが。

2016-08-05 22:25:41.739 MapTest[1844:443449] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

言われるまま、request..メソッドを足します。

let lm = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    lm.requestWhenInUseAuthorization()
    lm.startUpdatingLocation()
}

が、これでも現在地が表示されません。そもそも、位置情報利用を許可するためのダイアログも出ません。もう一手間必要なんですね。Info.plistに以下のプロパティを足してやります。

NSLocationWhenInUseUsageDescription

値はなんでも良いのですが、ダイアログの下部に表示されます(添付画像の「Hello MapKit」の部分)。

IMG_2067.jpg

これで晴れて現在地が表示されます!

IMG_2068.jpg

参考

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5