ZverArt
ZverArt

Reputation: 245

How to get latitude and longitude of a user's location in iOS

I am a new in Swift and I need to get the current location of a user. I mean that I need to get latitude and longitude. I have tried this:

class ViewController: UIViewController, CLLocationManagerDelegate{

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in

            if (error != nil) {
                println("ERROR:" + error.localizedDescription)
                return
            }

            if placemarks.count > 0 {
                let pm = placemarks[0] as CLPlacemark
                self.displayLocationInfo(pm)
            } else {
                println("Error with data")
            }
        })
    }

    func displayLocationInfo(placemark: CLPlacemark) {
        //  self.locationManager.stopUpdatingLocation()

        println(placemark.locality)
        println(placemark.postalCode)
        println(placemark.administrativeArea)
        println(placemark.country)
        println(placemark.location)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
        println("Error:" + error.localizedDescription)
    }
}

And here I can get coordinates, but it will looks like:

<+55.75590390,+37.61744720> +/- 100.00m (speed -1.00 mps / course -1.00) @ 2/14/15, 10:48:14 AM Moscow Standard Time

How can I retrieve only latitude and longitude?

Upvotes: 4

Views: 7670

Answers (3)

Rizwan Ahmed
Rizwan Ahmed

Reputation: 958

Code updated for Swift 3.x and Swift 4.x

As I can see you have used print(placemark.location) in your code.

So if you want to get only latitude use this code

print(placemark.location.coordinate.latitude)

and if you want to get only longitude use this code

print(placemark.location.coordinate.longitude)

Hope this helps !

Upvotes: 3

Pham Hoan
Pham Hoan

Reputation: 2127

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)

Your locations:[AnyObject]! actually is an [CLLocation] just get its last object and use CLLocation's coordinate property.

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/index.html

Upvotes: 1

Paulw11
Paulw11

Reputation: 114773

You can expect multiple calls to didUpdateLocations with accuracy improving over time (assuming you are in a local where the GPS can get good reception - outdoors, not surrounded by tall buildings). You can access the latitude and longitude directly from the CLLocation objects that are in the locations array.

let location = locations[locations.count-1] as CLLocation;
println("\(location.latitude) \(location.longitude)");

Upvotes: 2

Related Questions