0

我实际上正在使用核心位置和地图工具包框架来获取用户点击按钮的位置。我有两个视图控制器。第一个是带有地图视图的普通视图控制器,所有核心定位过程都在其中完成,以及反向地理编码。第二个是表格视图控制器,它显示从地理编码和位置的纬度和经度获得的数据。

我得到的问题是我无法将当前位置的信息传递给下一个视图。但是,我可以传递我的地理编码值。这对我来说似乎很奇怪,任何帮助将不胜感激。

mapViewController.m

#import "mapViewController.h"

@interface mapViewController ()
{
    CLLocationManager *locationManager;
    CLGeocoder *geoCoder;
    CLPlacemark *placemark;
    addressViewController *view;
}

@end

@implementation mapViewController
@synthesize mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    locationManager = [[CLLocationManager alloc]init];
    geoCoder = [[CLGeocoder alloc]init];
    self.addressOutlet.enabled = NO;
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"addressSegue"])
    {
        view= segue.destinationViewController;

         // assigning the addressPlacemark the same value as the current placemark
        view.addressPlacemark = placemark;

        NSLog(@"this is perfectly executed");

    }
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 600, 600);
    [self.mapView setRegion:region animated:YES];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newOne = [locations lastObject];

    [locationManager stopUpdatingLocation];
    NSLog(@"the new location is : %@", newOne);
    NSLog(@"the latitude is %f", newOne.coordinate.latitude);
    NSLog(@"the speed is %.2f mps", newOne.speed);
    NSLog(@"at time : %@",newOne.timestamp);

//These 4 NSLog statements are returning the outputs as expected


 [geoCoder reverseGeocodeLocation:newOne completionHandler:^(NSArray *placemarks, NSError *error) {
        placemark = [placemarks objectAtIndex:0];
        NSLog(@"the current city is %@",placemark.locality);

        self.addressOutlet.enabled = YES;
        // view.currentLocation = [[CLLocation alloc]init];

        view.currentLocation = newOne;  // assigning currentLocation the same location as the newOne

        NSLog(@"the currentLocation object has the location : \n %@",view.currentLocation);
        //problem : the output of this NSLog gives Null
    }];

}
4

2 回答 2

1

问题在于

view.currentLocation = newOne;

在这种情况下,视图为零。当prepareForSegue:被调用时,视图被实例化。所以你能做的是

tempLocation = newOne; // Store newOne in a temporary variable

并且在

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"addressSegue"])
{
    view= segue.destinationViewController;

     // assigning the addressPlacemark the same value as the current placemark
    view.addressPlacemark = placemark;
     view.currentLocation = tempLocation;
    NSLog(@"this is perfectly executed");

}
}
于 2013-04-10T13:44:39.047 回答
0
  1. 您不应该保存addressViewController在当前控制器中,因为它会保留他直到您再次执行 segue。并且在某些执行点上,它至少是无用的,甚至是有害的。
  2. view.currentLocation = newOne; // assigning currentLocation the same location as the newOne 此时的视图可以包含 nill,因此对其属性的任何分配都不正确。您应该保存currentLocation在当前视图控制器的属性中,然后prepareForSegue将其设置为destinationViewController.
  3. 我不想谈论您的代码命名约定,但是当我看到以小写字母开头的类名时,我感到很困惑=)
于 2013-04-10T13:38:13.303 回答