6

I'm trying to get city name from my current location coordiate by using CLGeocoder().reverseGeocodeLocation.

It gives me country name, street name, state and many other things but not city. Is there anything wrong with my code?

Here's my code:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    CLGeocoder().reverseGeocodeLocation(location) { (placeMark, error) in
        if error != nil{
            print("Some errors: \(String(describing: error?.localizedDescription))")
        }else{
            if let place = placeMark?[0]{
                print("country: \(place.administrativeArea)")

                self.lblCurrentLocation.text = place.administrativeArea
            }
        }
    } }

I use the below code too. But doesn't work for me. Here's the another way.

        let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!)
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]

        // Address dictionary
        print(placeMark.addressDictionary as Any)

        // Location name
        if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
            print("locationName: \(locationName)")
        }
        // Street address
        if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
            print("street: \(street)")
        }
        // City
        if let city = placeMark.addressDictionary!["City"] as? NSString {
            print("city : \(city)")
        }
        // Zip code
        if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
            print("zip :\(zip)")
        }
        // Country
        if let country = placeMark.addressDictionary!["Country"] as? NSString {
            print("country :\(country)")
        }
    })

Please someone help me to get city name.

Uday Babariya
  • 1,031
  • 1
  • 13
  • 31
Sptibo
  • 281
  • 2
  • 8
  • 22
  • Doesn't the output of the line `print(placeMark.addressDictionary as Any)` give a hint? – vadian Mar 14 '18 at 11:19
  • Which latitude and longitude do you use ? – Nikunj Damani Mar 14 '18 at 11:40
  • (self.locationManager.location?.coordinate.latitude)!, (self.locationManager.location?.coordinate.longitude)! I have used currend location latitude and longitude. Yes the drop down hint gives many options but non of them gives the city name. – Sptibo Mar 14 '18 at 13:09

1 Answers1

8

The field is called locality

 if let locality = placeMark.addressDictionary!["locality"] as? NSString {
            print("locality :\(locality)")
        }

Locality Apple docs

https://developer.apple.com/documentation/corelocation/clplacemark/1423507-locality?language=objc

CLPlacemark

https://developer.apple.com/documentation/corelocation/clplacemark?language=objc

Update:

Try this

import Foundation
import CoreLocation

let geoCoder = CLGeocoder()
let location = CLLocation(latitude: 40.730610, longitude:  -73.935242) // <- New York

geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, _) -> Void in

    placemarks?.forEach { (placemark) in

        if let city = placemark.locality { print(city) } // Prints "New York"
    }
})
brduca
  • 3,573
  • 2
  • 22
  • 30
  • It doesn't execute the print line. Above code is printing street, country, locationName but not city. I've tried ["Locality"] and ["locality"] too. – Sptibo Mar 14 '18 at 13:45
  • It prints "New York" on above mentioned coordinate but doesn't work on Nepal's coordinate. – Sptibo Mar 14 '18 at 16:55
  • It means that this location is not complete on Apple's register. But the field is the one I told. In this case there is nothing to do. – brduca Mar 14 '18 at 18:40