1

私のプロジェクトでは、マップに追加する複数のタイル ソースがあります。タイル ソースごとにボタンがあります。ボタンが最初に押されたときに addTileSource になり、次回は removeTileSource になり、この方法で交互に続けます。私の問題は、 if ステートメントの前にオブジェクトを初期化するため、ボタンを押すたびに作成される別の myTileSource を削除しているため、 removeTileSource が削除されないことです。どうすればこれを解決できますか? 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");
    }
}

最初にボタンを押すと、マップが読み込まれ、「YES」と表示されます。2回目に押すとマップが消えず「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 に答える