While
developing the iOS applications developer uses the locations to
display on the map in their apps. So they have to understand the
process of the geocoding and reverse geocoding. So that they can
apply this effectively in their apps. This blog will provide the easy
understanding of the Geocoding and Reverse Geocoding.
Geocoding
–
Geocoding is the process of converting address (New York, NY, USA)
into the geographical coordinates(40.7127837,-74.0059413). In the iOS
it is provided by the CLGeocoder
class of CoreLocation
framework.
CLGeocoder
*geocoder = [[CLGeocoder
alloc]init];
[geocoder
geocodeAddressString:addressString
completionHandler:^(NSArray*
placemarks, NSError*
error)
{
if
([placemarks count]>0)
{
CLPlacemark
*clPlace = [placemarks objectAtIndex:0];
CLLocation
*location = clPlace.location;
}
}];
CLPlacemark
provides following properties through which we can get more details
of a location.
- name
- addressDictionary
- ISOcountryCode
- country
- postalCode
- administrativeArea //The state or province associated with the placemark
- subAdministrativeArea //Additional administrative area information for the placemark
- locality //city
- subLocality //Additional city-level information for the placemark.
- thoroughfare
- subThoroughfare
- region
Reverse
Geocoding - Reverse
geocoding is the
process of converting geographic coordinates(40.7127837,-74.0059413) into
a human-readable address(New York, NY, USA). To get the address information iOS
provides CLGeocoder class for above iOS 5 version. For iOS 5 and below iOS provides the MKReverseGeocoder class.
For
iOS 5 above
CLLocation
*location = [[CLLocation
alloc]initWithLatitude:latCord
longitude:longCord];
CLGeocoder
*geocoder = [[CLGeocoder
alloc]
init];
[geocoder
reverseGeocodeLocation:location
completionHandler:
^(NSArray*
placemarks, NSError*
error)
{
if
([placemarks count]
> 0)
{
CLPlacemark
*placeMark = [placemarks objectAtIndex:0];
}
}];
Using
iOS 5 and below using MKReverseGeocoder
class.
MKReverseGeocoder*
geocoder = [[MKReverseGeocoder
alloc]
initWithCoordinate:location.coordinate];
geocoder.delegate
= self;
[geocoder start];
-
(void)reverseGeocoder:(MKReverseGeocoder*)geocoder
didFindPlacemark:(MKPlacemark*)place
{
NSLog(@"%@",place.addressDictionary);
}
-
(void)reverseGeocoder:(MKReverseGeocoder*)geocoder
didFailWithError:(NSError*)error {
NSLog(@"Error
occured");
}
Using
Google API – We can also reverse geocode using the google api.
NSString
*requestUrl=[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json? latlng=%f,%f&sensor=false",latCord,longCord];
requestUrl =
[requestUrl stringByReplacingOccurrencesOfString:@"
" withString:@"%20"];
NSMutableURLRequest
*request = [[[NSMutableURLRequest
alloc]
init]
autorelease];
[request
setTimeoutInterval:60];
[request setURL:[NSURL
URLWithString:requestUrl]];
[request
setHTTPMethod:@"GET"];
[request
addValue:@"application/JSON"
forHTTPHeaderField:@"Content-Type"];
NSURLResponse*
response = nil;
NSError*
error = nil;
NSString
*responseString = nil;
NSData
*result = [NSURLConnection
sendSynchronousRequest:request
returningResponse:&response
error:&error];
if
(error)
{
NSLog(@"Error
occured");
}else
{
NSMutableDictionary
*dicResult = [NSJSONSerialization
JSONObjectWithData:result
options:
NSJSONReadingMutableContainers
error:
&error];
if
([[dicResult objectForKey:@"status"]
isEqualToString:@"OK"])
{
NSArray
*arrayResult = [dicResult objectForKey:@"results"];
if
([arrayResult count]>0)
{
NSLog(@"%@",[arrayResult
objectAtIndex:0]);
}
}
}
No comments:
Post a Comment