Help us understand the problem. What is going on with this article?

Swiftで現在地を取得してみた

More than 3 years have passed since last update.

Swiftで現在地を取得するのにかなり手間取ったので、メモを残しておきます。

現在地の緯度と経度を画面に表示させています。

ViewController.swift
import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var lm: CLLocationManager! = nil
    var longitude: CLLocationDegrees!
    var latitude: CLLocationDegrees!


    @IBOutlet var lonLabel: UILabel
    @IBOutlet var latLabel: UILabel

    @IBAction func nowButton(sender: AnyObject) {
        //現在地取得
        lm.startUpdatingLocation()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var url = NSURL(scheme: "http", host: "xxxxxxx.herokuapp.com", path: "/1.0/users")
        var request = NSURLRequest(URL: url)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: nil) as NSDictionary

        for (key, value) in json {
            println()
        }

        longitude = 0.0
        latitude = 0.0
        lonLabel.text = String(longitude)
        latLabel.text = String(latitude)


        lm = CLLocationManager()
        lm.delegate = self
        //位置情報取得の可否。バックグラウンドで実行中の場合にもアプリが位置情報を利用することを許可する
        lm.requestAlwaysAuthorization()
        //位置情報の精度
        lm.desiredAccuracy = kCLLocationAccuracyBest
        //位置情報取得間隔(m)
        lm.distanceFilter = 300
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /** 位置情報取得成功時 */
    func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!){
        longitude = newLocation.coordinate.logitude
        latitude = newLocation.coordinate.latitude

        self.lonLabel.text = String(longitude)
        self.latLabel.text = String(latitude)
    }

        /** 位置情報取得失敗時 */
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        NSLog("Error")
    }

}


これだけでいけると思っていたんですが、iOS8からだと、info.plistに以下のkeyが存在しないとlocationManagerメソッドが呼ばれないようです。

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>

参考:
http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working

Stack Overflow様々ですm(_ _)m

ftsan
locondo
靴とファッションの通販サイト「ロコンド.jp」の開発・運営をしています。
http://www.locondo.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away