Android Question Offline maps for google maps

yfleury

Active Member
Licensed User
Longtime User
Is it possible to load offline map for my app using google maps when I am not connected to internet. My app doesn't need to be connected to internet, except when i am at home with wifi.
 

TILogistic

Expert
Licensed User
Longtime User
It can be helpful.
 
Upvote 0

yfleury

Active Member
Licensed User
Longtime User
Thank @Omar Parra A.
I check your link and osm look to have all a need. But it doesn't have satelit image and maps is very light for detail. Look at osm and google maps

I have to stay with Google map and Googlemapsextra.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
change:

for googlemaps:
j.Download($"https://mt1.google.com/vt/lyrs=r&x=${aX}&y=${aY}&z=${aZ}"$)
B4X:
'Get Tile from internet openstreemap website
public Sub getTileFromInternet(aZ As Int,aX As Int,aY As Int) As ResumableSub
    Dim j As HttpJob
    Dim bmp As B4XBitmap
    Try
        j.Initialize("", Me)
'        j.Download($"https://a.tile.openstreetmap.org/${aZ}/${aX}/${aY}.png"$)
        j.Download($"https://mt1.google.com/vt/lyrs=r&x=${aX}&y=${aY}&z=${aZ}"$)
        j.GetRequest.SetHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Try
                bmp=j.GetBitmap
            Catch
                Log(LastException.Message)
            End Try
        End If
    Catch
        Log(LastException.Message)
    End Try
    If j.IsInitialized Then
        j.Release
    End If
    Return bmp
End Sub

1659963300315.png
 
Upvote 0

kgf

Member
Licensed User
If you want to stay with Google maps then you can use custom tile provider to show mbtiles that will show offline. You will need to create them yourself and have the file on the device. High zooms and large areas will mean large files though.

Also note that the 'Get_MbTile' event wont fire under debug mode, it will only show tiles in release mode.

B4X:
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
 
Last edited:
Upvote 0

yfleury

Active Member
Licensed User
Longtime User
thanks all.
All of that make me busy for a long time, but I don't have so mush time for now. I keep your work here for the next update of my app. For now, I create app demo for a client then I can connect to internet by mobile. I bookmark this threads
 
Upvote 0
Top