0

I am actually using core location along with map kit framework to get the user location on a button's click. I have two view controllers. The first one is a normal view controller with the map view and all the core location processes done in it along with reverse geo coding. The second one is a table view controller that displays the obtained data from geo coding and location's latitude and longitude.

The problem which I get is that I am not able to pass the information of the current location to the next view. But, I can pass my geo coding values. It seems strange for me and any help would be much appreciated.

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
    }];

}
Raj0689
  • 73
  • 1
  • 1
  • 9

2 Answers2

1

The problem lies in

view.currentLocation = newOne;

In this context view is nil. view is instantiated when prepareForSegue: is getting called. So what you can do is

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

and in

-(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");

}
}
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0
  1. You should not save addressViewController in current controller because it retain him till you perform segue again. And in some point of execution it will be at least useless or even harmful.
  2. view.currentLocation = newOne; // assigning currentLocation the same location as the newOne view at this point can contain nill, so any assigns to it's properties do not correct. You should save currentLocation in current view controller's property and then in prepareForSegue set it to destinationViewController.
  3. I don't want to talk about your code naming convention, but it confused me when I saw class name which start from lower case letter=)
Ossir
  • 3,109
  • 1
  • 34
  • 52
  • I understand @Ossir for the naming convention. I am going to change them completely as you have mentioned. I followed them like properties which I shouldn't have done. Thanks for pointing that out. Why did not I realise it earlier. And, I am new to programming and hence these errors :( – Raj0689 Apr 10 '13 at 14:14