我可以使用已保存的坐标进行反向地理编码吗?

时间:2014-05-08 03:08:59

标签: ios location reverse-geocoding

我有一个应用程序,通过反向地理编码使用以下内容为用户提供当前位置。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

NSLog(@"Location: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {

    latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    //end hear for just long lat
}

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error == nil && [placemarks count] >0) {

        placemark = [placemarks lastObject];

        self.addressLable.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                  placemark.subThoroughfare, placemark.thoroughfare,
                                  placemark.postalCode, placemark.locality,
                                  placemark.administrativeArea,
                                  placemark.country];
        streetandNo = [NSString stringWithFormat:@"%@ %@", placemark.subThoroughfare,placemark.thoroughfare];
        suberb = [NSString stringWithFormat:@"%@", placemark.locality];
    }else {

        NSLog(@"%@", error.debugDescription);
    }

}];
[self performSelector:@selector(stopupdate) withObject:nil afterDelay:2.0];

}

这完美无缺。我想知道的是有一种方法可以提供先前保存的位置坐标,然后基本上运行相同的过程吗?我查看了苹果文档,我能找到的每件事都指的是使用当前位置。我正在跳跃,有一种方法可以在CLLocation下创建一个位置并设置我自己的long和Latt,但我似乎无法找到引导我朝正确方向前进的参考。

1 个答案:

答案 0 :(得分:0)

float longitude = Your saved longitude;
float latitude = Your saved latitude;

CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

现在在代码中使用location而不是currentLocation

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error == nil && [placemarks count] >0) {

    placemark = [placemarks lastObject];

    self.addressLable.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                              placemark.subThoroughfare, placemark.thoroughfare,
                              placemark.postalCode, placemark.locality,
                              placemark.administrativeArea,
                              placemark.country];
    streetandNo = [NSString stringWithFormat:@"%@ %@", placemark.subThoroughfare,placemark.thoroughfare];
    suberb = [NSString stringWithFormat:@"%@", placemark.locality];
}else {

    NSLog(@"%@", error.debugDescription);
}

}];

可能适合你。