Location Label

I have a label in my app I wanted to know how to make it so the label knows the location. Whether it be the location set in the setting or if it is nothing is set in the setting it goes to the current location automatically.

Accepted Reply

So, use CLGeocoder().reverseGeocodeLocation(location, completionHandler: }


    func getPlacemarkFromLocation(_ location: CLLocation) {
       
        CLGeocoder().reverseGeocodeLocation(location, completionHandler:
            {(placemarks, error) in
                if (error != nil) {
                    // print("reverse geodcode fail: \(error.localizedDescription)")
                }      
                if let _ = error  {
                } else {
                    if let pm = placemarks {  
                        if pm.count > 0 {  
                            self.findPhysicalLocation(location, placemark:placemarks![0])  
                        }
                    }
                }
        })
    }


findPhysicalLocation converts to labels :


    func findPhysicalLocation(_ aLocation: CLLocation, placemark: CLPlacemark) {
      
        if GlobalData.sharedGlobal.forceLocalisation { // on demande de localiser, sinon on ne fait rien
            if let localite = placemark.locality {
                let country = placemark.country
               // use them as needed
                myPresentLongitude = aLocation.coordinate.longitude
                myPresentLatitude = aLocation.coordinate.latitude       // If need them
            }
        }
    }

Here are all the properties in CLPlacemark


var name: String?

The name of the placemark.


var isoCountryCode: String?

The abbreviated country name.


var country: String?

The name of the country associated with the placemark.


var postalCode: String?

The postal code associated with the placemark.


var administrativeArea: String?

The state or province associated with the placemark.


var subAdministrativeArea: String?

Additional administrative area information for the placemark.


var locality: String?

The city associated with the placemark.


var subLocality: String?

Additional city-level information for the placemark.


var thoroughfare: String?

The street address associated with the placemark.


var subThoroughfare: String?

Additional street-level information for the placemark.


var region: CLRegion?

The geographic region associated with the placemark.


var timeZone: TimeZone?

The time zone associated with the placemark.




Replies

This is not a question about a feature of the Swift programming language, so it’s posted in the wrong forum. Getting Started or Location and Maps might be better.


A label doesn’t “know” anything. It’s a dumb, one-way, UI widget that can *display* things. It’s the view controller that has to “know” the location and update the label.


Anyway, the place to start would be Core Location.

How do you want to display location ? As coordinates or as city, country…


for coordinates, you could have 3 labels (or one grouping the 3)

    @IBOutlet var latitudeLabel: UILabel!    
    @IBOutlet var longitudeLabel: UILabel!
    @IBOutlet var altitudeLabel: UILabel!


then in the locationManger func


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      
        let newLocation = locations[locations.count - 1]
        displayLongAndLat(location: newLocation)
}

displaying with:

    func displayLongAndLat(location aLocation: CLLocation) { // 24.3.15

        var latitudeString = String(format: "%g\u{00B0}", aLocation.coordinate.latitude) 
        if aLocation.coordinate.latitude >= 0.0 {
            latitudeString = latitudeString + " N"
        } else {
            latitudeString = latitudeString + " S"
        }
        latitudeLabel.text = latitudeString
}



To get the physical address, use


CLGeocoder().reverseGeocodeLocation(location, completionHandler: }

I want to display the location as the street address, zip code, and city

So, use CLGeocoder().reverseGeocodeLocation(location, completionHandler: }


    func getPlacemarkFromLocation(_ location: CLLocation) {
       
        CLGeocoder().reverseGeocodeLocation(location, completionHandler:
            {(placemarks, error) in
                if (error != nil) {
                    // print("reverse geodcode fail: \(error.localizedDescription)")
                }      
                if let _ = error  {
                } else {
                    if let pm = placemarks {  
                        if pm.count > 0 {  
                            self.findPhysicalLocation(location, placemark:placemarks![0])  
                        }
                    }
                }
        })
    }


findPhysicalLocation converts to labels :


    func findPhysicalLocation(_ aLocation: CLLocation, placemark: CLPlacemark) {
      
        if GlobalData.sharedGlobal.forceLocalisation { // on demande de localiser, sinon on ne fait rien
            if let localite = placemark.locality {
                let country = placemark.country
               // use them as needed
                myPresentLongitude = aLocation.coordinate.longitude
                myPresentLatitude = aLocation.coordinate.latitude       // If need them
            }
        }
    }

Here are all the properties in CLPlacemark


var name: String?

The name of the placemark.


var isoCountryCode: String?

The abbreviated country name.


var country: String?

The name of the country associated with the placemark.


var postalCode: String?

The postal code associated with the placemark.


var administrativeArea: String?

The state or province associated with the placemark.


var subAdministrativeArea: String?

Additional administrative area information for the placemark.


var locality: String?

The city associated with the placemark.


var subLocality: String?

Additional city-level information for the placemark.


var thoroughfare: String?

The street address associated with the placemark.


var subThoroughfare: String?

Additional street-level information for the placemark.


var region: CLRegion?

The geographic region associated with the placemark.


var timeZone: TimeZone?

The time zone associated with the placemark.




How would I make it so that if I changed it from a label to a button? The button would display the location in the title, and if the user tapped it would allow them to choose if they want to automatically get the location, and make the changes needed to the automatic location or if they want to type in the location themselves.

Could someone please respond to my previous response.

If you have a way to create the text for a label, it seems easy to use this to set a button title.

When I put this code:


func getPlacemarkFromLocation(_ location: CLLocation) {  
         
        CLGeocoder().reverseGeocodeLocation(location, completionHandler:  
            {(placemarks, error) in  
                if (error != nil) {  
                    // print("reverse geodcode fail: \(error.localizedDescription)")  
                }        
                if let _ = error  {  
                } else {  
                    if let pm = placemarks {    
                        if pm.count > 0 {    
                            self.findPhysicalLocation(location, placemark:placemarks![0])    
                        }  
                    }  
                }  
        })  
    } 

I get this error:


Use of undeclared type 'CLLocation' on line 01


Use of unresolved identifier 'CLGeocoder' on line 03

Did you

import CoreLocation