Sub Process_Globals
Dim LastCameraPosition As CameraPosition
Dim PreferenceManager1 As AHPreferenceManager
Dim PreferenceScreen1 As AHPreferenceScreen
End Sub
Sub Globals
Dim GoogleMap1 As GoogleMap
Dim LastMapLatitude As Double
Dim LastMapLongitude As Double
Dim LastMapZoom As Float
Dim MapDatabaseName As String="kings_lynn.map"
Dim MapFragment1 As MapFragment
Dim MapPanel As Panel
Dim MapsForgeTileOverlay As TileOverlay
Dim MapsForgeTileProvider1 As MapsForgeTileProvider
Dim TextScale As Float
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
Activity.AddMenuItem3("Settings", "Settings", Null, True)
If MapFragment1.IsGooglePlayServicesAvailable = False Then
ToastMessageShow("Google Play services not available", True)
Else
If FirstTime Then
If Not(File.Exists(File.DirDefaultExternal, MapDatabaseName)) Then
' copy the mapsforge database to default package directory
File.Copy(File.DirAssets, MapDatabaseName, File.DirDefaultExternal, MapDatabaseName)
End If
CreatePreferences
End If
MapFragment1.Initialize("MapFragment1", MapPanel)
End If
End Sub
Sub Activity_Resume
' check if we are returning to this Activity from the AHPreferenceActivity and a preference has been changed
Dim UpdatedKeys As List=PreferenceManager1.GetUpdatedKeys
If GoogleMap1.IsInitialized AND UpdatedKeys.Size>0 Then
' 1 or more preference values have been updated
If UpdatedKeys.IndexOf("MapType")<>-1 Then
' MapType preference has been updated
Dim MapType As Int=PreferenceManager1.GetString("MapType")
If MapType<>GoogleMap1.MapType Then
GoogleMap1.MapType=MapType
' if the default or saved map type is MAP_TYPE_NONE we will display mapsforge tiles
' otherwise we'll display a built in visible google map type
' hide the mapsforge tiles if the default or saved map type is a built in visible google map type
If MapType=GoogleMap1.MAP_TYPE_NONE Then
MapsForgeTileOverlay.Visible=True
Else
MapsForgeTileOverlay.Visible=False
End If
End If
End If
If UpdatedKeys.IndexOf("TextScale")<>-1 Then
' TextScale preference has been updated
Dim NewTextScale As Float=PreferenceManager1.GetString("TextScale")
If NewTextScale<>TextScale Then
TextScale=NewTextScale
MapsForgeTileProvider1.SetTextScale(TextScale)
' clear existing tiles so new tiles are rendered with the new text scale
MapsForgeTileOverlay.ClearTileCache
End If
End If
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If GoogleMap1.IsInitialized Then
' save the CameraPosition, map center and zoom level
LastCameraPosition=GoogleMap1.CameraPosition
PreferenceManager1.SetString("Latitude", GoogleMap1.CameraPosition.Target.Latitude)
PreferenceManager1.SetString("Longitude", GoogleMap1.CameraPosition.Target.Longitude)
PreferenceManager1.SetString("Zoom", GoogleMap1.CameraPosition.Zoom)
End If
End Sub
Sub BitmapToByteArray(Bitmap1 As Bitmap) As Byte()
Dim OutputStream1 As OutputStream
Dim ByteArray1() As Byte
OutputStream1.InitializeToBytesArray(1)
Bitmap1.WriteToStream(OutputStream1, 100, "PNG")
ByteArray1 = OutputStream1.ToBytesArray
OutputStream1.Close
Return ByteArray1
End Sub
Sub CreatePreferences
' note i've added a line to the manifest to include the AHPreferenceActivity
' version 1.04+ of AHPreferenceActivity is required
PreferenceScreen1.Initialize("Settings", "")
Dim Category1 As AHPreferenceCategory
Category1.Initialize("Map")
' MapTypes is a b4a Map object whose keys are String representations of Integer MapTypes and values are String names of the MapType
' (the AHPreferenceActivity handles only String and Boolean types so we must use Strings for the Integer MapType values)
Dim MapTypes As Map
MapTypes.Initialize
Dim StringValues(5) As String=Array As String(GoogleMap1.MAP_TYPE_HYBRID, GoogleMap1.MAP_TYPE_NONE, GoogleMap1.MAP_TYPE_NORMAL, GoogleMap1.MAP_TYPE_SATELLITE, GoogleMap1.MAP_TYPE_TERRAIN)
MapTypes.Put(StringValues(0), "Hybrid")
MapTypes.Put(StringValues(1), "MapsForge")
MapTypes.Put(StringValues(2), "Normal")
MapTypes.Put(StringValues(3), "Satellite")
MapTypes.Put(StringValues(4), "Terrain")
Category1.AddList2("MapType", "Map Type", "Select source of map tiles", StringValues(1), "", MapTypes)
' 12290 is INPUT_TYPE_DECIMAL_NUMBERS
Category1.AddEditText2("TextScale", "Text Scale", "How much to scale the text on rendered tiles", "1.0", 12290, False, True, "")
PreferenceScreen1.AddPreferenceCategory(Category1)
If PreferenceManager1.GetAll.Size = 0 Then
PreferenceManager1.SetString("MapType", StringValues(1))
PreferenceManager1.SetString("TextScale", "1.0")
' i've used the PreferenceManager to save map center and zoom
' these preferences aren't exposed in the AHPreferenceActivity
' set the default map center and zoom
PreferenceManager1.SetString("Latitude", "52.75619")
PreferenceManager1.SetString("Longitude", "0.3980")
PreferenceManager1.SetString("Zoom", "14")
End If
PreferenceManager1.GetUpdatedKeys
End Sub
Sub MapFragment1_Ready
Log("MapFragment1_Ready")
GoogleMap1=MapFragment1.GetMap
If GoogleMap1.IsInitialized = False Then
ToastMessageShow("Error initializing map", True)
Else
' get the default or last saved map type
Dim MapType As Int=PreferenceManager1.GetString("MapType")
GoogleMap1.MapType=MapType
Dim GoogleMapsExtras1 As GoogleMapsExtras
Dim NoTileTile As Tile
Dim TileOverlayOptions1 As TileOverlayOptions
MapsForgeTileProvider1.GetMapsForgeTileProviderOptions.SetMapDatabaseFile(File.DirDefaultExternal, MapDatabaseName)
' optional - set a tile to be displayed where there are no mapsforge tiles
NoTileTile.Initialize(256, 256, BitmapToByteArray(LoadBitmap(File.DirAssets, "no_tiles.png")))
MapsForgeTileProvider1.GetMapsForgeTileProviderOptions.SetNoTileTile(NoTileTile)
' optional - but in this demo we want to set the default text scale or restore the last text scale
TextScale=PreferenceManager1.GetString("TextScale")
MapsForgeTileProvider1.GetMapsForgeTileProviderOptions.SetTextScale(TextScale)
' the following single line syntax is also possible
' MapsForgeTileProvider1.GetMapsForgeTileProviderOptions.SetMapDatabaseFile(File.DirDefaultExternal, MapDatabaseName).SetNoTileTile(NoTileTile).SetTextScale(TextScale)
' now that we have set the required (and some optional) MapsForgeTileProviderOptions we can call the MapsForgeTileProvider Initialize method
MapsForgeTileProvider1.Initialize
TileOverlayOptions1.Initialize
TileOverlayOptions1.SetTileProvider(MapsForgeTileProvider1)
If MapType<>GoogleMap1.MAP_TYPE_NONE Then
' if the default or saved map type is MAP_TYPE_NONE we will display mapsforge tiles
' otherwise we'll display a built in visible google map type
' hide the mapsforge tiles if the default or saved map type is a built in visible google map type
TileOverlayOptions1.SetVisible(False)
End If
MapsForgeTileOverlay=GoogleMapsExtras1.AddTileOverlay(GoogleMap1, TileOverlayOptions1)
GoogleMap1.AddMarker(52.75619, 0.3980, "Home Sweet Home")
If LastCameraPosition.IsInitialized Then
' restore the last saved CameraPosition
GoogleMap1.AnimateCamera(LastCameraPosition)
Else
' set the default or last saved map center and zoom level
LastMapLatitude=PreferenceManager1.GetString("Latitude")
LastMapLongitude=PreferenceManager1.GetString("Longitude")
LastMapZoom=PreferenceManager1.GetString("Zoom")
' center the map on the default or last saved map center and zoom
Dim CameraPosition1 As CameraPosition
CameraPosition1.Initialize(LastMapLatitude, LastMapLongitude, LastMapZoom)
GoogleMap1.AnimateCamera(CameraPosition1)
End If
End If
End Sub
Sub Settings_Click
StartActivity(PreferenceScreen1.CreateIntent)
End Sub