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;
}
}