Unable to get city name by current latitude and longitude in swift
Asked Answered
H

1

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.

Hectocotylus answered 14/3, 2018 at 11:3 Comment(3)
Doesn't the output of the line print(placeMark.addressDictionary as Any) give a hint?Lung
Which latitude and longitude do you use ?Durning
(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.Hectocotylus
F
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"
    }
})
Finfoot answered 14/3, 2018 at 11:16 Comment(3)
It doesn't execute the print line. Above code is printing street, country, locationName but not city. I've tried ["Locality"] and ["locality"] too.Hectocotylus
It prints "New York" on above mentioned coordinate but doesn't work on Nepal's coordinate.Hectocotylus
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.Finfoot

© 2022 - 2024 — McMap. All rights reserved.