Dim sqlTiles As SQL
Sub AddMbTiles(filepath As String, filename As String)
'Log("AddMbTiles " & filepath & " " & filename)
If File.Exists(filepath, filename) Then
Log(filename & " is found")
sqlTiles.Initialize(filepath, filename, False)
CustomTileProvider1.Initialize(B4XPages.MainPage.PageMap, "Get_MbTile")
TileOverlayOptions1.Initialize
TileOverlayOptions1.SetTileProvider(CustomTileProvider1)
' sets to back so that polygons etc show
TileOverlayOptions1.SetZIndex(-1)
TileOverlay1 = GoogleMapsExtras1.AddTileOverlay(GoogleMap1, TileOverlayOptions1)
' Log("CustomTileProvider1.IsInitialized " & CustomTileProvider1.IsInitialized)
Else
Log(filename & " NOT found")
TileOverlay1.Remove
End If
End Sub
Sub Get_MbTile(TileX As Int, TileY As Int, Zoom As Int) As Tile
'Log("Get_MbTile X="&TileX&", Y"&TileY&", Zoom="&Zoom)
Dim Cursor1 As Cursor
Dim Buffer() As Byte
Dim newY As Int = adjustTmsY(TileY, Zoom)
Dim strSql As String = "Select tile_data from tiles where zoom_level = " & Zoom & " AND [tile_column] = " & TileX & " AND [tile_row] = " & newY & ";"
'Log(strSql)
Cursor1 = sqlTiles.ExecQuery(strSql)
Cursor1.Position = 0
Buffer = Cursor1.GetBlob("tile_data")
'Log("Buffer.Length " & Buffer.Length)
If Buffer.Length>1 Then
'Log("MbTile Found at X="&TileX&", newY"&newY&", Zoom="&Zoom)
Dim Tile1 As Tile
Tile1.Initialize(256,256,Buffer)
'Cursor1.Close
Return Tile1
Else
'Cursor1.Close
Return CustomTileProvider1.NO_TILE
End If
End Sub
Sub adjustTmsY(Y As Int, Zoom As Int) As Int
'Invert tile y origin from top To bottom of map google TMS to Mbtiles Y reference
'https://alastaira.wordpress.com/2011/07/06/converting-tms-tile-coordinates-to-googlebingosm-tile-coordinates/
Dim ymax As Int = Bit.shiftleft(1,Zoom)
Return ymax - Y - 1
End Sub