149
145

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-07-19

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

149
145
3

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
149
145

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?