Android Question [SOLVED] OpenStreetMap example

RB Smissaert

Well-Known Member
Licensed User
Are there any clear instructions available how to use an offline OpenStreetMap in B4A? I only need a small area of the UK.
Not sure how to download the map data I need and how to process and store it.
Currently I am using OSMDroid (4.1) with different map data, but I am unable to get up to data map data with this now.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Are there any clear instructions available how to use an offline OpenStreetMap in B4A? I only need a small area of the UK.
Not sure how to download the map data I need and how to process and store it.
Currently I am using OSMDroid (4.1) with different map data, but I am unable to get up to data map data with this now.

RBS
At least I can see now I can download smaller areas from Geofabrik. Just need to figure out how to convert and to what.

RBS
 

PamazFit

Member
Licensed User
Hi,

Hope this can be of help:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    ' OSM 4.1 library used:
    ' OSMDroid_4_1
    ' OSMDroid_4_1_MapsForgeTileSource
    Dim MapView1 As OSMDroid_MapView   

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim rp As RuntimePermissions
    Dim SafeDir As String = rp.GetSafeDirDefaultExternal("")
    
    Dim MapsForgeDatabaseName As String = "andorra-v3.map"
    Dim MapsForgeDatabasePath As String = SafeDir
    
    If File.Exists (MapsForgeDatabasePath, MapsForgeDatabaseName) = False Then
        File.Copy(File.DirAssets, MapsForgeDatabaseName, MapsForgeDatabasePath, MapsForgeDatabaseName)
    End If
    
    MapView1.Initialize("MapView")
    
    Dim MFTS As MapsForgeTileSource
    MFTS.GetMapsForgeTileSourceOptions.SetMapDatabaseFile(MapsForgeDatabasePath, MapsForgeDatabaseName)
    MFTS.GetMapsForgeTileSourceOptions.SetTileSourceName("MyMap")
    MFTS.Initialize
        
    MapView1.SetBuiltInZoomControls(True)
    MapView1.SetMultiTouchControls(True)
    MapView1.GetController.SetZoom(14)
    MapView1.SetTileSource(MFTS)   
    
    Dim GeoPoint As OSMDroid_GeoPoint
    Dim lat As Double = 42.5464   ' Andora
    Dim lon As Double = 1.5141
    GeoPoint.Initialize(lat, lon)
    MapView1.GetController.SetCenter(GeoPoint)
    
    Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
End Sub

See also here and here for other examples

Note that you can't open a map directly from the assests folder.

maps download (use the v3 maps)
http://download.mapsforge.org/maps/

You should also read this and this thread with lots of other examples from warwound (the creator of these fantastic libraries)


Kind regards,
Patrick
 

RB Smissaert

Well-Known Member
Licensed User
Hi,

Hope this can be of help:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    ' OSM 4.1 library used:
    ' OSMDroid_4_1
    ' OSMDroid_4_1_MapsForgeTileSource
    Dim MapView1 As OSMDroid_MapView  

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim rp As RuntimePermissions
    Dim SafeDir As String = rp.GetSafeDirDefaultExternal("")
   
    Dim MapsForgeDatabaseName As String = "andorra-v3.map"
    Dim MapsForgeDatabasePath As String = SafeDir
   
    If File.Exists (MapsForgeDatabasePath, MapsForgeDatabaseName) = False Then
        File.Copy(File.DirAssets, MapsForgeDatabaseName, MapsForgeDatabasePath, MapsForgeDatabaseName)
    End If
   
    MapView1.Initialize("MapView")
   
    Dim MFTS As MapsForgeTileSource
    MFTS.GetMapsForgeTileSourceOptions.SetMapDatabaseFile(MapsForgeDatabasePath, MapsForgeDatabaseName)
    MFTS.GetMapsForgeTileSourceOptions.SetTileSourceName("MyMap")
    MFTS.Initialize
       
    MapView1.SetBuiltInZoomControls(True)
    MapView1.SetMultiTouchControls(True)
    MapView1.GetController.SetZoom(14)
    MapView1.SetTileSource(MFTS)  
   
    Dim GeoPoint As OSMDroid_GeoPoint
    Dim lat As Double = 42.5464   ' Andora
    Dim lon As Double = 1.5141
    GeoPoint.Initialize(lat, lon)
    MapView1.GetController.SetCenter(GeoPoint)
   
    Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
End Sub

See also here and here for other examples

Note that you can't open a map directly from the assests folder.

maps download (use the v3 maps)
http://download.mapsforge.org/maps/

You should also read this and this thread with lots of other examples from warwound (the creator of these fantastic libraries)


Kind regards,
Patrick
Thanks, will have a look at all that.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Are there any clear instructions available how to use an offline OpenStreetMap in B4A? I only need a small area of the UK.
Not sure how to download the map data I need and how to process and store it.
Currently I am using OSMDroid (4.1) with different map data, but I am unable to get up to data map data with this now.

RBS
Are there any clear instructions available how to use an offline OpenStreetMap in B4A? I only need a small area of the UK.
Not sure how to download the map data I need and how to process and store it.
Currently I am using OSMDroid (4.1) with different map data, but I am unable to get up to data map data with this now.

RBS
Got this all worked out now and for in case somebody needs it these are the steps to make a mbtiles SQLite file of the required area of OpenStreetMap:

1. Download and install Maperitive: http://maperitive.net/
2. Start Maperitive by running the .exe
3. Tools, Add Web map, OSM Mapnik
4. Move to the required area on the map
5. Right-click the map and do Place geometry bounds
6. Drag the margins to the required area
7. Right-click and do Dump this position
8. Map (in the menu) and do Download OSM data
9. Copy this (alter as required) and paste to the command prompt box at the bottom:
generate-mbtiles bitmap-type=png exclude-partial=false file=Mapnik.mbtiles map-name=Mapnik maxzoom=19 minzoom=12 subpixel=2
Not sure about the subpixel argument, the default 1 might be fine.
10. Click in the command prompt box and hit the Return key
The generated SQLite file will be dumped to the Maperitive folder.

11. The table tiles in the generated SQLite file as no index and this is needed to avoid the tiles being loaded very slowly.
One single compound index involving all fields except the data blob field will do eg:
CREATE UNIQUE INDEX idx_tiles_zoom_level_tile_column_tile_row on tiles(zoom_level, tile_column, tile_row)
Load the SQLite file in whatever SQLite editor you use and create the above index.

The Maperitive interface is confusing and it took me some time to work the above out, hence the post to maybe avoid somebody having the same trouble.

RBS
 
Top