Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

I am tying to get iPhone's coordinates for an API key . Please help.

So I'm tying to get the coordinates of an iPhone ( simulator) but it gives me an error every time. Can someone tell me good code for this ( Swift 2.0 , Xcode 7.0.1) Thanks in advance.

Can you show the code you have tried so far?

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate {

     let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()


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

    }

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

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


    }

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




}

Caleb Kleveter

What is your error?

That was the one without errors , this is the one with lots of errors :

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate {

     let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()


        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        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 {
                print("Reverse geocoder failed with error" + error.localizedDescription)
                return
            }

            if placemarks.count > 0 {
                let pm = placemarks[0] as CLPlacemark
                self.displayLocationInfo(pm)
            } else {
                print("Problem with the data received from geocoder")
            }
        })
    }

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

    func displayLocationInfo(placemark: CLPlacemark) {
        if placemark != nil {
            //stop updating location to save battery life
            locationManager.stopUpdatingLocation()
            print(containsPlacemark.location.coordinate.latitude)
            print(containsPlacemark.location.coordinate.longitude)






}

1 Answer

Without knowing what your exact errors are, and where they occur, this seems to compile within a Playground

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // Using wrong method?
        CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error) -> Void in
            if error != nil {
                print("Reverse geocoder failed with error" + error!.localizedDescription) // Must unwrap 'error'
                return
            }

            if placemarks!.count > 0 { // Must unwrap 'placemarks'
                let pm:CLPlacemark = placemarks!.first! // Must unwrap 'placemarks'
                self.displayLocationInfo(pm)
            } else {
                print("Problem with the data received from geocoder")
            }
        })
    }

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

    func displayLocationInfo(placemark: CLPlacemark?) // Need '?' to make Optional
    {
        if placemark != nil {
            //stop updating location to save battery life
            locationManager.stopUpdatingLocation()
            print(containsPlacemark.location.coordinate.latitude) // Is 'containsPlacemark.location....' your own method?
            print(containsPlacemark.location.coordinate.longitude)
        }
    }

It's best you resubmit your code including comments with the exact errors your seeing, next to the exact lines of code where they're being thrown.