Get text description of region from GPS coordinates
I am using Core Location in iOS to get GPS coordinates. I would like to get some human readable text descri开发者_运维技巧ption of the general region where these coordinates are. I can do this either locally on the device itself or on a PHP-based website to which I am submitting the coordinates.
Any ideas?
you can create URL's to retrieve a text desription of a set of coordinates like this:
http://maps.google.com/maps/geo?q="latitude","longtitude"&output=csv&sensor=false
E.G http://maps.google.com/maps/geo?q=37.42228990140251,-122.0822035425683&output=csv&sensor=false
You can do the reverse geocoding in iOs on the device itself using the MKReverseGeocoder : http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/Reference/Reference.html#//apple_ref/occ/cl/MKReverseGeocoder
You can see some sample usage here.
I use CLLocationManagerDelegate and MKReverseGeocoderDelegate in my app. Create CLLocationManager * locationManager set up its properties and accuracy then start.
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
[manager stopUpdatingHeading];
CLLocationCoordinate2D coordinate = manager.location.coordinate;
MKReverseGeocoder * geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
geocoder.delegate = self;
[geocoder start];
}
#pragma mark - MKReverseGeocoderDelegate
You'll get NSDictionary with location info. I've used no all keys. If you need more than listed NSLOG dictionary keys and its responsive values. Hope it will help you.
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
getLocationState = glsAvailable;
NSDictionary * dic = placemark.addressDictionary;
NSString * CountryCode = [dic objectForKey:@"CountryCode"];
NSString * State = [dic objectForKey:@"State"];
NSString * City = [dic objectForKey:@"City"];
NSString * SubLocality = [dic objectForKey:@"SubLocality"];
NSString * Street = [dic objectForKey:@"Street"];
NSString * ZIP = [dic objectForKey:@"ZIP"];
self.locationString = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",
CountryCode?CountryCode:@"",
State?State:@"",
City?City:@"",
SubLocality?SubLocality:@"",
Street?Street:@"",
ZIP?ZIP:@""
];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LOCATION_STRING_IS_READY" object:nil];
[geocoder cancel];
[geocoder release];
}
精彩评论