0

我正在尝试从我在应用程序中获得的 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];

 }];
4

1 回答 1

2

是的。使用保存的纬度/经度值的方法创建一个CLLocation对象initWithLatitude:longitude:,并将其传递给reverseGeocodeLocation:.

我很惊讶您说这是有效的(尽管,如果您在模拟器上,无论如何都会模拟位置服务,这可能是原因),因为当您调用时startUpdatingLocation,您的 CLLocationManagerDelegate 方法的实现locationManager:didUpdateToLocation:fromLocation:会被调用。(你实现了这些对吗?)只有当这个(和其他)委托方法被调用时,你才能确定你已经成功地确定了用户的位置。

您可能需要阅读Apple 记录的CLLocationManagerDelegate协议和定位服务最佳实践。

于 2012-09-06T19:04:42.277 回答