-1

我遇到了一些新手问题。

我得到了这种方法来根据 GPS 坐标确定 iPhone 所在的邮政编码。反向地理编码。我的方法有效,我知道这一点,因为将正确的邮政编码写到标签上没有问题。

但我需要将此邮政编码存储在一个字符串中,以便在我用来编写 SMS 的方法中使用该字符串。

任何人都可以帮助我吗?

告诉我是否需要源代码!(只是目前没有坐在我的工作补偿上)

显然我被误解了,应该发布 som 代码。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;


    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            **postalCode** = [NSString stringWithFormat:@"%@",
                            placemark.postalCode];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

在这里,我尝试将邮政编码保存在名为 postalCode 的 NSString 中(以“**”突出显示)

我尝试在短信作曲家中再次加载它

-(void)displaySMSComposerSheet
{
    CLLocation *currentLocation;


    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            postalCode = [NSString stringWithFormat:@"%@",
                          placemark.postalCode];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.recipients = [NSArray arrayWithObjects:@"1220", nil];
    picker.body = [NSString stringWithFormat:@"Dog %@", (placemark.)postalCode];
    picker.messageComposeDelegate = self;

    [self presentModalViewController:picker animated:YES];

}

仅在 SMS 窗口中打印: Dog (null) 我确定我有坐标,它们会在输出中打印出来。

希望这有助于更好地理解这个问题

4

1 回答 1

1
#import <CoreLocation/CoreLocation.h>

@interface SomeController : UIViewController <CLLocationManagerDelegate>
@property (strong,nonatomic) CLLocationManager *locationManager;
@end


@implementation SomeController

-(void) trackUpdates
{    
    self.locationManager = [CLLocationManager new];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    self.locationManager.distanceFilter = 10.0f;
    if ([CLLocationManager locationServicesEnabled]){
        [self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)locations 
{
    CLGeocoder* gcrev = [[CLGeocoder alloc] init];
    [gcrev reverseGeocodeLocation:[locations lastObject] 
                completionHandler:^(NSArray *placemarks, NSError *error)
     {
         CLPlacemark* revMark = [placemarks objectAtIndex:0];
         NSString *zip = [revMark.addressDictionary objectForKey:@"ZIP"];
         NSLog(@"%@",zip);
         // ... do something with the zip, store in an ivar, call a method, etc.
     }];
}

-(void)viewDidLoad {
    [super viewDidLoad];
    [self trackUpdates];
}

@end
于 2013-04-03T08:04:10.070 回答