LoginSignup
2
2

More than 5 years have passed since last update.

[iOS] Get the location use gps

Last updated at Posted at 2015-04-16

First add the CoreLocation.framework to the project.
Then use the following code to get the location:

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager:CLLocationManager! = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    }
    func locationManager(manager: CLLocationManager!,
        didUpdateToLocation newLocation: CLLocation!,   
        fromLocation oldLocation: CLLocation!) {

        let longitude = newLocation.coordinate.longitude
        let latitude = newLocation.coordinate.latitude

        longitudeLabel.text = longitude.description
        latitudeLabel.text = latitude.description

        //If you just want to get once, use this code
        //to stop the updating location.
        locationManager.stopUpdatingLocation()
    }
}
2
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
2
2