0
votes

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:

2
You should post your solution as an answer and accept your own answer.Jano
Answer added with the solution, thankssidneivl
Where is the answer? I am having the same issueGeorg

2 Answers

0
votes

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];
0
votes

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!