地图视图不更新相机位置,也不使用 animateToLocation

Map View not updating camera position, neither with animateToLocation

本文很长,在此先感谢您。

我有两个视图控制器:

  1. MainViewController:有一个 Google 带有搜索文本字段的地图视图,用户在其中输入城市名称并将位置更改为该城市,我使用一种方法将城市名称转换为经纬度使用来自 google 的地理编码服务。这里一切正常。
  2. SelectAreaViewController:这个 VC 是使用推送 segue 访问的,它有一个带有 8 个简单行(区域)的选择器视图。当用户选择一行时,它会自动关闭,然后它会在 MainViewController 上执行一个方法来更新地图视图位置。这是问题所在。该方法已执行,但地图视图位置未更新。我试过使用 animateToLocation 并设置 "camera" 属性 但它不起作用。

我什至尝试在加载视图时执行该方法并且它工作正常,所以问题是当从另一个调用该方法时 viewcontroller,我想这是一个 segue 问题,但我不能在 google 或 SO.

上找到任何内容

这是应该更新位置的 MainViewController 方法。我再说一遍,该方法已执行,但地图视图不响应位置更改方法 (animateToLocation) 或相机设置。

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

}

第二个viewcontroller中的方法是这样的:

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

    [mainvc setPlazaOnMap:row];


    [self.navigationController popViewControllerAnimated:YES];
}

再次感谢。

你可以尝试改变你的

self.mapView.camera = camera;

self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; self.view = self.mapView;

当您的应用程序启动时,您在导航控制器的堆栈上有一个 MainViewController 实例。

然后将 SelectViewController 的一个实例压入堆栈。

在 didSelectRow 方法中弹出 SelectViewController,这会将您带回到 MainViewController 的原始实例。

问题是您的 didSelectRow 方法正在分配 MainViewController 的新实例,然后在该实例上调用 setPlazaOnMap,而不是在原始实例上调用它。

要获取原始实例,您需要这样的东西:

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

然后在上面调用 setPlazaOnMap。