xcode 当我们知道纬度和经度时,如何以字符串形式获取当前位置名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11288815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:45:17  来源:igfitidea点击:

How to get the current Location name as a string , when we know the latitude and longitude

iphoneobjective-ciosxcodelatitude-longitude

提问by Ravi

Possible Duplicate:
How to retrieve user's current city name?

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

using the following methods i am getting latitude and longitude of the current position

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

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

now i need to get the current location name as a string... but not required on a map, how can i do this, give me some suggestion pls,,,thanx in advance

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

回答by Krishnabhadra

You are asking about Reverse Geocoding.. Use MKReverseGeocodingfor this purpose.. ThisSO question explains it a bit..

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

回答by sElanthiraiyan

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

回答by Mehul Mistri

Use this common function and just pass latitude and longitude as argument in this function.

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

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

and declare this

并声明这个

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

This function will return address of the latitude and longitude you are passing.

此函数将返回您正在传递的纬度和经度的地址。

Hope It helps.

希望能帮助到你。

回答by EXC_BAD_ACCESS

You can use the google API to get the address. Probably you have to send lat and long value as parameter.

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

Use google reverse geoCoding

使用谷歌反向地理编码

sample http call here http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

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

回答by Shantanu

Maybe this is what you will have to use MKReverseGeocoder

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