BigLex
BigLex

Reputation: 3028

Is there a way to get the street name from a CLLocation in iOS?

In my app I'd need to know the name of the street the user is in. At the moment I only know hot to get the user location with the CLLocationManger object and to show it in a MKMapView but I can't find any way to get the name of the street where the user is.

Is there a way to do this with or without the iOS sdk?

Upvotes: 2

Views: 1290

Answers (2)

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61784

Simple extension:

import CoreLocation

typealias StreetNameHandler = (String?) -> Void

extension CLLocation {

    func streetNameWithCompletionBlock(completionBlock: StreetNameHandler) {

        CLGeocoder().reverseGeocodeLocation(self) { placemarks, error in

            if let addressDictionary = placemarks?.first?.addressDictionary, street = addressDictionary["Street"] as? String {
                completionBlock(street)
            }
        }
    }
}

Simple usage:

location.streetNameWithCompletionBlock { street in
    print("street \(street)")
}

Upvotes: 0

Fran Sevillano
Fran Sevillano

Reputation: 8163

From iOS 5 and on, you can do this making use of CLGeocoder. I strongly advise you to take a look at the Location Awareness Programming Guide, here.

In order to get the street, you should make a request using reverseGeocodeLocation:completionHandler:. In that completion handler you will receive an array of CLPlacemark objects. To get the street, just extract the object from the addressDictionary dictionary in the CLPlacemark object using the kABPersonAddressStreetKey key.

Upvotes: 3

Related Questions