原问题来自于CSDN问答频道,详细解决方案见:http://ask.csdn.net/questions/1706问题描述:
通过使用CLLocationManager得到当前城市和国家的名称。
解决方案:
- (void) getReverseGeocode
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; if(currentLatLong.count > 0)
{
CLLocationCoordinate2D myCoOrdinate; myCoOrdinate.latitude = LatValue;
myCoOrdinate.longitude = LangValue; CLLocation *location = [[CLLocation alloc] initWithLatitude:myCoOrdinate.latitude longitude:myCoOrdinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *places, NSError *error)
{
if (error)
{
NSLog(@"failed with error: %@", error);
return;
}
if(places.count > 0)
{
NSString *MyAddress = @"";
NSString *city = @""; if([place.addressDictionary objectForKey:@"FormattedAddressLines"] != NULL)
MyAddress = [[place.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
else
MyAddress = @"Address Not founded"; if([place.addressDictionary objectForKey:@"SubAdministrativeArea"] != NULL)
city = [place.addressDictionary objectForKey:@"SubAdministrativeArea"];
else if([place.addressDictionary objectForKey:@"City"] != NULL)
city = [place.addressDictionary objectForKey:@"City"];
else if([place.addressDictionary objectForKey:@"Country"] != NULL)
city = [place.addressDictionary objectForKey:@"Country"];
else
city = @"City Not founded"; NSLog(@"%@",city);
NSLog(@"%@", MyAddress);
}
}];
}
}
通过使用CLLocationManager得到当前城市和国家的名称。
解决方案:
- (void) getReverseGeocode
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; if(currentLatLong.count > 0)
{
CLLocationCoordinate2D myCoOrdinate; myCoOrdinate.latitude = LatValue;
myCoOrdinate.longitude = LangValue; CLLocation *location = [[CLLocation alloc] initWithLatitude:myCoOrdinate.latitude longitude:myCoOrdinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *places, NSError *error)
{
if (error)
{
NSLog(@"failed with error: %@", error);
return;
}
if(places.count > 0)
{
NSString *MyAddress = @"";
NSString *city = @""; if([place.addressDictionary objectForKey:@"FormattedAddressLines"] != NULL)
MyAddress = [[place.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
else
MyAddress = @"Address Not founded"; if([place.addressDictionary objectForKey:@"SubAdministrativeArea"] != NULL)
city = [place.addressDictionary objectForKey:@"SubAdministrativeArea"];
else if([place.addressDictionary objectForKey:@"City"] != NULL)
city = [place.addressDictionary objectForKey:@"City"];
else if([place.addressDictionary objectForKey:@"Country"] != NULL)
city = [place.addressDictionary objectForKey:@"Country"];
else
city = @"City Not founded"; NSLog(@"%@",city);
NSLog(@"%@", MyAddress);
}
}];
}
}
怀念谷歌地图时代