1

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

使用以下方法我得到当前位置的纬度和经度

- (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;
}

现在我需要以字符串形式获取当前位置名称...但在地图上不需要,我该怎么做,请提前给我一些建议,,,thanx

4

5 回答 5

3

您在询问反向地理编码..为此目的使用MKReverseGeocoding ..这个SO 问题解释了一点..

于 2012-07-02T06:45:41.220 回答
3
- (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);
        }    
    }];
}
于 2012-07-02T06:48:51.310 回答
3

使用这个通用函数,只需在这个函数中传递纬度和经度作为参数。

-(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"

此函数将返回您所经过的纬度和经度的地址。

希望能帮助到你。

于 2012-07-02T07:03:33.527 回答
2

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

使用谷歌反向地理编码

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

于 2012-07-02T06:47:52.530 回答
0

也许这就是您必须使用的 MKReverseGeocoder

于 2012-07-02T06:45:58.510 回答