Easiest Way of Getting Reverse Geocoded Current Location from iOS

Easiest way of getting reverse geocoded current location from iOS

I would start with the CLReverseGeocoder class.

This stackoverflow question gets the current city and can probably be adapted for your use.

ios: What is the efficient and recommended way to reverse geocode?

You can use CLGeocoder:

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error){
CLPlacemark *placemark = placemarks[0];
NSLog(@"Found %@", placemark.name);
}];

This will still take time though, since both methods use web services to convert lat / long into a place

Reverse Geocoding Current User Location, How To?

I just wrote this simple function that will get the user's location from the mapView and attempt to reverse geocode it. Code and comments are pretty straight-forward. Let me know if you have questions.

- (void)reverseGeocodeUserLocation {
// create your geocoder object
CLGeocoder *geocoder = [[CLGeocoder alloc] init];

// get the user's location from the map view
CLLocation *userLocation = self.mapView.userLocation.location;

// tell the geocoder to reverse geocode the user's location
[geocoder reverseGeocodeLocation:userLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// if there was an error
if (error) {
// handle error here...
return;
}

// get the first "placemark" object from the result array
CLPlacemark *firstPlacemark = [placemarks firstObject];

// get the address dictionary from the placemark object
NSDictionary *addressDictionary = firstPlacemark.addressDictionary;

// break the dictionary into components for display if you'd like
NSString *street = addressDictionary[@"Street"];
NSString *city = addressDictionary[@"City"];
NSString *state = addressDictionary[@"State"];
NSString *zip = addressDictionary[@"Zip"];
}];
}

I simulated a location to test this out, the coordinates were 42.9897, -71.45435, (somewhat nearby where I am) and the result dictionary I got from the first placemark object was:

{
City = Manchester;
Country = "United States";
CountryCode = US;
FormattedAddressLines = (
"274 Merrimack St",
"Manchester, NH 03103-4721",
"United States"
);
PostCodeExtension = 4721;
State = NH;
Street = "274 Merrimack St";
SubAdministrativeArea = Hillsborough;
SubLocality = "East End";
SubThoroughfare = 274;
Thoroughfare = "Merrimack St";
ZIP = 03103;
}

Reverse Geocode Current Location

You can use MKReverseGeocoder from 3.0 through 5.0. Since 5.0 MKReverseGeocoder is depreciated and usage of CLGeocoder is advised.

You should use CLGeocoder if available. In order to be able to extract address information you would have to include Address Book framework.

#import <AddressBookUI/AddressBookUI.h>
#import <CoreLocation/CLGeocoder.h>
#import <CoreLocation/CLPlacemark.h>

- (void)reverseGeocodeLocation:(CLLocation *)location
{
CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init];
if (reverseGeocoder) {
[reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark* placemark = [placemarks firstObject];
if (placemark) {
//Using blocks, get zip code
NSString* zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
}
}];
}else{
MKReverseGeocoder* rev = [[MKReverseGeocoder alloc] initWithCoordinate:location.coordinate];
rev.delegate = self;//using delegate
[rev start];
//[rev release]; release when appropriate
}
//[reverseGeocoder release];release when appropriate
}

MKReverseGeocoder delegate method:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
//Get zip code
NSString* zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
}

MKReverseGeocoder and ABPersonAddressZIPKey were deprecated in iOS 9.0. Instead the postalcode property of the CLPlacemark can be used to get zip code:

NSString * zipCode = placemark.postalCode;

Reverse Geocode Location in Swift

you never reverse geocode the location but you pass in manager.location.

see:
CLGeocoder().reverseGeocodeLocation(manager.location, ...

I assume that was a copy&paste mistake and that this is the issue - the code itself looks good - almost ;)

working code

    var longitude :CLLocationDegrees = -122.0312186
var latitude :CLLocationDegrees = 37.33233141

var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!
println(location)

CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
println(location)
guard error == nil else {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
guard placemarks.count > 0 else {
println("Problem with the data received from geocoder")
return
}
let pm = placemarks[0] as! CLPlacemark
println(pm.locality)
})

CLGeocoder reverse geocode from given location

I never tried this, but can't you create your own CLLocation object?

If you know current longitude and latitude you can -

CLLocation *location = [[CLLocation alloc]initWithLatitude:someValue longitude:someValue];
[self.geoCoder reverseGeocodeLocation: location completionHandler: ^(NSArray *placemarks, NSError *error) {
//do something
});

Is it possible to get more precise reverse geocoding?

You can checkout all the properties you can get here:

https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLPlacemark_class/index.html

get address from reverse geocoding

Use MKReverseGeocoder. Look at the Apple sample app CurrentAddress.

The placemark object passed into didFindPlacemark has the address fields which you can put into a string.



Related Topics



Leave a reply



Submit