我正在尝试从我之前在应用中获得的Lat / Long值反转地理编码位置,我希望从此坐标中找到城市名称,国家/地区名称和ISO。
我目前正在使用CLLocationManager通过以下代码获取实际位置信息:
//Auto geolocation and find city/country
locationManager.delegate=self;
//Get user location
[locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
locatedAtcountry = placemark.country;
locatedAtcity = placemark.locality;
locatedAtisocountry = placemark.ISOcountryCode;
//Print the location to console
NSLog(@"Estas en %@",locatedAtcountry);
NSLog(@"Estas en %@",locatedAtcity);
NSLog(@"Estas en %@",locatedAtisocountry);
[cityLabel setText:[NSString stringWithFormat:@"%@,",locatedAtcity]];
[locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]];
//Set the label text to current location
//[locationLabel setText:locatedAt];
}];
它工作得很好但是,可以从我已经保存在设备中的Long / Lat值中做同样的事情,而不是像实际代码那样使用当前位置吗?
更新和解决方案:
感谢Mark 的答案,我最后使用以下代码从保存的坐标中获取信息:
CLLocation *location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700];
[self.geoCoder reverseGeocodeLocation: location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
locatedAtcountry = placemark.country;
locatedAtcity = placemark.locality;
locatedAtisocountry = placemark.ISOcountryCode;
//Print the location to console
NSLog(@"Estas en %@",locatedAtcountry);
NSLog(@"Estas en %@",locatedAtcity);
NSLog(@"Estas en %@",locatedAtisocountry);
[cityLabel setText:[NSString stringWithFormat:@"%@",locatedAtcity]];
[locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]];
//Set the label text to current location
//[locationLabel setText:locatedAt];
}];
答案 0 :(得分:2)
是。使用保存的纬度/经度值使用CLLocation
方法创建initWithLatitude:longitude:
对象,并将其传递给reverseGeocodeLocation:
。
我很惊讶您说这是有效的(尽管,如果您在模拟器上,无论如何都会模拟位置服务,这可能是原因),因为当您调用startUpdatingLocation
时,您的CLLocationManagerDelegate方法的实现像locationManager:didUpdateToLocation:fromLocation:
被叫。 (您已经实现了这些权利吗?)只有在调用此(和其他)委托方法时,您才可以确定已成功确定用户的位置。
您可能希望阅读Apple记录的CLLocationManagerDelegate
协议和Location Services最佳做法。