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

位置情報取得 swift2.0

More than 5 years have passed since last update.
  • swift >= 2.0
  • Deployment Target == 9.1

corelocation.framework をLinked
info.plistに NSLocationAlwaysUsageDescription:String(アプリの説明文) を追加

ViewController.swift
import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

  var lm: CLLocationManager! = nil

  override func viewDidLoad() {
    super.viewDidLoad()

    lm = CLLocationManager()
    lm.delegate = self

    lm.requestAlwaysAuthorization()
    lm.desiredAccuracy = kCLLocationAccuracyBest
    lm.distanceFilter = 300
    lm.startUpdatingLocation()
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  /** 位置情報取得 成功 */
  func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation){

    print("緯度:"+String(newLocation.coordinate.latitude))
    print("経度:"+String(newLocation.coordinate.longitude))
  }

  /** 位置情報取得 失敗 */
  func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error")
  }
}
muchi_ko
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