1

在我的项目中,我有多个添加到地图的图块源。我为每个图块源设置了一个按钮。我希望第一次按下按钮时 addTileSource ,然后下一次 removeTileSource 并继续以这种方式交替。我的问题是它不会 removeTileSource 因为它正在删除每次按下按钮时创建的不同 myTileSource ,因为我在 if 语句之前初始化了对象。我该如何解决这个问题?我尝试在 viewDidLoad 和 if 语句中初始化平铺源,但在我调用它的其他位置出现“使用未声明的标识符”错误。请查看我的代码并就如何实现预期目标提出建议。谢谢你的时间。

- (IBAction)LayerButton:(id)sender 
{
    RMMBTilesSource *myTileSource = [[RMMBTilesSource alloc] initWithTileSetURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MapName" ofType:@"mbtiles"]]];
    FirstViewController *FVC = [self.tabBarController.viewControllers objectAtIndex:0];
    self.phase3BIsChecked = !self.phase3BIsChecked;

    if((self.phase3BIsChecked)) {
        [[FVC mapView] addTileSource:myTileSource];
        self.phase3BButtonView.backgroundColor = [UIColor blueColor];
    } else {
        self.phase3BButtonView.backgroundColor = [UIColor lightGrayColor];
        [[FVC mapView] removeTileSource:myTileSource];
    }

    NSLog(@"Map Index = %@", [[[FVC mapView] tileSources]  description]);
    if ([[[FVC mapView] tileSources] containsObject:myTileSource]) {
        NSLog(@"YES");
    } else {
        NSLog(@"NO");
    }
}

当我第一次按下按钮时,地图加载并且我得到“是”。当我第二次按下它时,地图没有关闭,我得到“NO”。这几乎总结了我的问题

4

1 回答 1

1

在视图控制器的接口定义中,添加此变量定义:

RMMBTilesSource *myTileSource;

在您的视图控制器中viewDidLoad,添加以下内容:

myTileSource = [[RMMBTilesSource alloc] initWithTileSetURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MapName" ofType:@"mbtiles"]]];

然后你的LayerButton动作可以变成这样:

- (IBAction)LayerButton:(id)sender {
    FirstViewController *FVC = [self.tabBarController.viewControllers objectAtIndex:0];
    self.phase3BIsChecked = !self.phase3BIsChecked;

    if((self.phase3BIsChecked)) {
        [[FVC mapView] addTileSource:myTileSource];
        self.phase3BButtonView.backgroundColor = [UIColor blueColor];
    } else {
        self.phase3BButtonView.backgroundColor = [UIColor lightGrayColor];
        [[FVC mapView] removeTileSource:myTileSource];
    }

    NSLog(@"Map Index = %@", [[[FVC mapView] tileSources]  description]);
    if ([[[FVC mapView] tileSources] containsObject:myTileSource]) {
        NSLog(@"YES");
    } else {
        NSLog(@"NO");
    }
}
于 2013-10-09T08:39:26.877 回答