Search code examples
iosobjective-cgoogle-mapsxcode6google-maps-sdk-ios

Map View not updating camera position, neither with animateToLocation


This is a long text so I thank you in advance.

I have two view controllers:

  1. MainViewController: has a Google Maps View with a search text field where user inputs a city name and the location is changed to that city, I use a method which translate the city name to latitude and longitude using the geocoding service from google. Here everything works fine.
  2. SelectAreaViewController: this VC is accessed using a push segue, it has a picker view with 8 simple rows (areas). When the user selects a row, it is automatically closed and then it executes a method on MainViewController to update the map view location. Here is the issue. The method is executed but the map view location is not updated. I've tried using animateToLocation and setting the "camera" property but it's not working.

I have even tried executing the method when view loads and it works OK, so the problem is when the method is called from the other viewcontroller, I guess it is a segue issue but I can't find anything on google or SO.

This is the MainViewController method which is supposed to update the location. I repeat, the method is executed, but the mapview is not responding to the location change method (animateToLocation) nor the camera settings.

-(void)setPlazaOnMap:(NSInteger)plazaNum
{
    double lat;
    double lng;
    NSString *plazaNombre;

    switch (plazaNum) {
        case 0:
            NSLog(@"Plaza 1");
            plazaNombre = @"Aguascalientes";
            lat = 21.8852562;
            lng = -102.2915677;
            break;

        case 1:
            NSLog(@"Plaza 2");
            plazaNombre = @"León";
            lat = 21.1250077;
            lng = -101.6859605;
            break;
        case 2:
            NSLog(@"Plaza 3");
            plazaNombre = @"San Luis Potosí";
            lat = 22.1564699;
            lng = -100.9855409;
            break;
        case 3:
            NSLog(@"Plaza 4");
            plazaNombre = @"Querétaro";
            lat = 20.5887932;
            lng = -100.3898881;
            break;
        case 4:
            NSLog(@"Plaza 5");
            plazaNombre = @"Monterrey";
            lat = 25.6866142;
            lng = -100.3161126;
            break;
        case 5:
            NSLog(@"Plaza 6");
            plazaNombre = @"Saltillo";
            lat = 25.4267244;
            lng = -100.9954254;
            break;
        case 6:
            NSLog(@"Plaza 7");
            plazaNombre = @"Torreón";
            lat = 25.5428443;
            lng = -103.4067861;
            break;
        case 7:
            NSLog(@"Plaza 8");
            plazaNombre = @"Cancún";
            lat = 21.161908;
            lng = -86.8515279;
            break;
        case 8:
            NSLog(@"Plaza 9");
            plazaNombre = @"Guadalajara";
            lat = 20.6596988;
            lng = -103.3496092;
            break;

        default:
            lat = 20.6596988;
            lng = -103.3496092;
            break;
    }

    NSLog(@"lat %f",lat);
    NSLog(@"lng %f",lng);


    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:lng zoom:12];
    self.mapView.camera = camera;

//  [self.mapView animateToLocation:CLLocationCoordinate2DMake(lat, lng)];
//  [self.mapView animateToZoom:10];

}

The method in the second viewcontroller is this:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    MainViewController *mainvc = [[MainViewController alloc] init];

    [mainvc setPlazaOnMap:row];


    [self.navigationController popViewControllerAnimated:YES];
}

Again, thanks in advance.


Solution

  • When your app starts up you have an instance of MainViewController on the navigation controller's stack.

    Then you push an instance of SelectViewController onto the stack.

    In your didSelectRow method you pop off the SelectViewController, which takes you back to the original instance of the MainViewController.

    The problem is that your didSelectRow method is allocating a new instance of MainViewController and then calling setPlazaOnMap on that, instead of calling it on the original instance.

    To get the original instance you'd need something like this:

    UIViewController* rootViewController = [self.navigationController.viewControllers objectAtIndex: 0];
    MainViewController* mainViewController = (MainViewController*)rootViewController;
    

    Then call setPlazaOnMap on that.