0

我遇到了一些新手问题。

我得到了这种方法来根据 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

3 回答 3

1

如果你想postalCode跨视图控制器访问,你可以把它保存到NSUserDefaults

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // ...
    NSString *postalCode = [NSString stringWithFormat:@"%@",
                            placemark.postalCode];
    [[NSUserDefaults standardDefaults] setObject:postalCode forKey:@"postalCode"];
    // ...
}

并再次加载

-(void)displaySMSComposerSheet
{
    // ...
    NSString *postalCode = [[NSUserDefaults standardDefaults] objectForKey:@"postalCode"];
    // ....
}

编辑:您确定placemark并且postalCode已经设置(即 -locationManager:didUpdateToLocation:fromLocation:已被调用)?如果displaySMSComposerSheet在确定位置之前调用,则此行为是正常的。

Edit2:从您的代码中,您似乎忘记告诉locationManager开始更新位置。将以下内容添加到viewDidLoad

locationManager.delegate = self;
[locationManager startUpdatingLocation];
于 2013-04-05T09:51:27.843 回答
0

尝试

picker.body = [NSString stringWithFormat:@"Dog %@", postalCode];
于 2013-04-05T09:57:46.413 回答
0
-(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];
            [self showMailComposer:postalcode]; // method call
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];



}

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

    [self presentModalViewController:picker animated:YES];
}
于 2013-04-05T11:06:18.403 回答