0
2

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】CoreLocationで現在地取得

Last updated at Posted at 2020-05-03

CoreLocationを使って現在地の緯度経度を一瞬取得するためのextensionです。

ViewController.swift
import CoreLocation
Viewcontroller.swift
override func viewDidLoad() {
        super.viewDidLoad()

        //CLLocationManagerDelegateのセットアップ
        locationManager.delegate = self
        //位置情報使ってもいい?
        locationManager.requestWhenInUseAuthorization()
        
    }
Viewcontroller.swift
extension ViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            
            locationManager.stopUpdatingLocation()
            var lat = location.coordinate.latitude
            var lon = location.coordinate.longitude
            //緯度・経度を渡す
            parseLocation(lat: lat, lon: lon)

        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
    
    
}
Viewcontroller.swift
@IBAction func buttonPressed(_ sender: UIButton) {
        //位置情報の取得開始
        locationManager.startUpdatingLocation()
    }
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?