0

I'm getting an error in my IOS application. I've searched in the google and here, but the specific solution was not found!

I have a viewController called mapView that I use in two moments in my app, this view contains a MKMapView and the code.

In my mapView.h there is:

@property (strong, nonatomic) IBOutlet MKMapView *mapSpot;

And in my mapView.m there is:

- (void)viewDidLoad {
    [super viewDidLoad];

    [mapSpot setShowsUserLocation:YES];
}

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

So, in the first moment I load the mapView into other ViewController using:

@property (strong, nonatomic) ViewMap *mapView;

mapView                         = [[ViewMap alloc] initWithNibName:@"ViewMap" bundle:nil];
[self.view addSubview:[mapView view]];

I unload that ViewController and in another ViewController in other moment I load the MapView again, but in this moment the method: - (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation not is called.

I verify if the first ViewController was unloaded and that was.

When I load the second ViewController there is a new instace of MapView, but not call the delegate method.

Anyone know something about that?

Thanks

==================================================================================

EDIT AND SOLVED:

sidneivl
  • 242
  • 3
  • 12

2 Answers2

0

the problem is in the way you are adding the view, in this line

[self.view addSubview:[mapView view]];

if you only add the view the controller code is not executed, instead of that you has to present the mapView, for example:

[self presentViewController:mapView animated:YES completion:nil];
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
  • that is not the problem, I tried use presentViewController and the problem not solved, and in iphone presentViewController load view in fullscreen, and it's not possible in my app. – sidneivl Feb 20 '13 at 18:38
  • Other question is, why work perfectly when I load the first time and when I reload the same view work, and when I change de view doesn't work? Thanks – sidneivl Feb 21 '13 at 00:40
0

The problem above, maybe happen because I'm using simulator to test app, and how the simulator not change the position map not get didUpdateUserLocation:

That's the unique explanation that I could have after the review the code, organize the classes read documentation and get error again.

Now, I'm using CLLocationManager to get position, after getting first time the position I stop it.

In the future I'll implement a system that track the user path, so using CLLocationManager is inevitable.

The mapView.m code after changes:

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager                     = [[CLLocationManager alloc] init];
    locationManager.delegate            = self;
    locationManager.distanceFilter      = kCLDistanceFilterNone;
    locationManager.desiredAccuracy     = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

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

    //  store the location to use in any moment, it needs to be checked because the first time when get the coordinate not pass infos to load places according the current position
    if (!location.latitude) {
        location                            = [loc coordinate];

//      set center the map at the current position
        MKCoordinateRegion region           = MKCoordinateRegionMakeWithDistance(location, 500, 500);
        [mapSpotView setRegion:region animated:YES];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"loadPlaces" object:nil];

        [locationManager stopUpdatingLocation];
    }
}

if someone has a better solution, please, post here!

That's it!

sidneivl
  • 242
  • 3
  • 12