10
11

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.

位置情報取得 swift2.0

Last updated at Posted at 2015-11-08
  • 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")
  }
}
10
11
2

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
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?