如何获取当前的位置名称作为一个string,当我们知道经度和纬度

可能重复:
如何检索用户的当前城市名称?

使用以下方法获取当前位置的纬度和经度

- (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { int degrees = newLocation.coordinate.latitude; double decimal = fabs(newLocation.coordinate.latitude - degrees); int minutes = decimal * 60; double seconds = decimal * 3600 - minutes * 60; NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; latLabel.text = lat; degrees = newLocation.coordinate.longitude; decimal = fabs(newLocation.coordinate.longitude - degrees); minutes = decimal * 60; seconds = decimal * 3600 - minutes * 60; NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", degrees, minutes, seconds]; longLabel.text = longt; } 

现在我需要得到当前的位置名称作为一个string…但不是在地图上要求,我怎么能做到这一点,给我一些build议,请提前,thanx

使用这个通用函数,只需将纬度和经度作为parameter passing给这个函数。

 -(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude { NSString *urlString = [NSString stringWithFormat:kGeoCodingString,pdblLatitude, pdblLongitude]; NSError* error; NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error]; locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""]; return [locationString substringFromIndex:6]; } 

并宣布这一点

 #define kGeoCodingString @"http://maps.google.com/maps/geo?q=%f,%f&output=csv" 

这个函数会返回你传递的经纬度的地址。

希望能帮助到你。

你正在问反向地理编码 ..使用MKReverseGeocoding这个目的.. 这个问题解释了一下..

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [self.locationManager stopUpdatingLocation]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * placemark in placemarks) { NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@", [placemark subThoroughfare],[placemark thoroughfare], [placemark locality], [placemark administrativeArea]]; NSLog(@"%@",addressTxt); } }]; } 

你可以使用谷歌API获取地址。 可能你必须发送lat和long值作为参数。

使用谷歌反向geoCoding

http的示例http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

也许这是你将不得不使用MKReverseGeocoder