MBTilesActivity : Switch between two (2) MBTile files for MBTilesTileDataSource

40 views
Skip to first unread message

rfidt...@gmail.com

unread,
Jan 22, 2017, 11:59:03 AM1/22/17
to Nutiteq-dev
I have two (2) MBTile files. I want to swicth view between these files.
I added a button to deal with it.
What I want, each click on this button alernatively displays my files in the same activity. I don'want to create a new activity, use one.
I tried to :
- remove baseLayer from map layers
- create a new MBTilesTileDataSource based on the new MBTile file
- create a new baselayer based on new MBTilesTileDataSource and added it to map layers.
then , I called mapView.forceLayout() and mapView.invalidate() to refresh view but
it does not work :(

aa...@cartodb.com

unread,
Jan 23, 2017, 3:35:54 AM1/23/17
to Nutiteq-dev, rfidt...@gmail.com
Hey,

You can either (1) recreate the layer each switch or (2) create both and change their visibility. The second requires more memory, but may be the better option depending on your specific case.

I've changed one of our existing samples to serve as a (rough) sample to you. I simply override back button to function as the switch and then follow a boolean flag to determine which file to use (my example uses the first option):

    public class BundledMapActivity: MapBaseActivity
     
{
         
protected override void OnCreate(Android.OS.Bundle savedInstanceState)
         
{
             
base.OnCreate(savedInstanceState);
 
             
AddOnlineBaseLayer(CartoBaseMapStyle.CartoBasemapStyleDefault);
 
             
TileDataSource source = CreateTileDataSource();
 
             
// Get decoder from current layer,
             
// so we wouldn't need a style asset to create a decoder from scratch
             
MBVectorTileDecoder decoder = (MBVectorTileDecoder)(MapView.Layers[0] as VectorTileLayer).TileDecoder;
 
             
// Remove default baselayer
             
MapView.Layers.Clear();
 
             
// Add our new layer
             
var layer = new VectorTileLayer(source, decoder);
 
             
MapView.Layers.Insert(0, layer);
 
             
// Zoom to the correct location
             
MapPos rome = BaseProjection.FromWgs84(new MapPos(12.4807, 41.8962));
             
MapView.SetFocusPos(rome, 0);
             
MapView.SetZoom(13, 0);
         
}
 
         
bool worldZoom;
 
         
public override void OnBackPressed()
         
{
             worldZoom
= !worldZoom;
 
             
TileDataSource source = CreateTileDataSource();
 
             
// Get decoder from current layer,
             
// so we wouldn't need a style asset to create a decoder from scratch
             
MBVectorTileDecoder decoder = (MBVectorTileDecoder)(MapView.Layers[0] as VectorTileLayer).TileDecoder;
 
             
// Remove default baselayer
             
MapView.Layers.Clear();
 
             
// Add our new layer
             
var layer = new VectorTileLayer(source, decoder);
 
             
MapView.Layers.Insert(0, layer);
         
}
 
         
TileDataSource CreateTileDataSource()
         
{
             
// offline map data source
             
string fileName = "rome_ntvt.mbtiles";
 
             
if (worldZoom) fileName = "world_zoom5.mbtiles";
 
             
try
             
{
                 
string directory = GetExternalFilesDir(null).ToString();
                 
string path = directory + "/" + fileName;
 
                 
Assets.CopyAssetToSDCard(fileName, path);
                 
Log.Debug("Copy done to " + path);
 
                 
MBTilesTileDataSource source = new MBTilesTileDataSource(0, 14, path);
                 
                 
return source;
             
}
             
catch (IOException e)
             
{
                 
Log.Debug("MbTileFile cannot be copied: " + fileName);
                 
Log.Debug("Message" + e.LocalizedMessage);
             
}
 
             
return null;
         
}
     
}

 

Best wishes,
Aare
CARTO Mobile
Reply all
Reply to author
Forward
0 new messages