我实际上正在使用核心位置和地图工具包框架来获取用户点击按钮的位置。我有两个视图控制器。第一个是带有地图视图的普通视图控制器,所有核心定位过程都在其中完成,以及反向地理编码。第二个是表格视图控制器,它显示从地理编码和位置的纬度和经度获得的数据。
我得到的问题是我无法将当前位置的信息传递给下一个视图。但是,我可以传递我的地理编码值。这对我来说似乎很奇怪,任何帮助将不胜感激。
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
}];
}