Android Tutorial OSMDroid - MapView for B4A tutorial

You can find the OSMDroid library thread here: http://www.b4x.com/forum/additional...tes/16309-osmdroid-mapview-b4a.html#post92643.

AIM: Create and initialize a MapView, enable the map zoom controller and multitouch controller, set a zoom level then center the map on a location.

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim MapView1 As MapView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If File.ExternalWritable=False Then
      '   OSMDroid requires the use of external storage to cache tiles
      '   if no external storage is available then the MapView will display no tiles
      Log("WARNING NO EXTERNAL STORAGE AVAILABLE")
   End If
   
   '   no EventName is required as we don't need to listen for MapView events
   MapView1.Initialize("")
   Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
   
   '   by default the map will zoom in on a double tap and also be draggable - no other user interface features are enabled
   
   '   enable the built in zoom controller - the map can now be zoomed in and out
   MapView1.SetZoomEnabled(True)
   
   '   enable the built in multi touch controller - the map can now be 'pinch zoomed'
   MapView1.SetMultiTouchEnabled(True)
   
   '   set the zoom level BEFORE the center (otherwise unpredictable map center may be set)
   MapView1.Zoom=14
   MapView1.SetCenter(52.75192, 0.40505)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

The code is pretty self-explanatory.

I've added the code to check if the device has available external storage as OSMDroid will not display any tiles if no external storage is available.
External storage is used to save/cache all downloaded tiles - no external storage means no map!
(I'll omit that check from future tutorials but it's something to bear in mind - not that i know of any Android devices that have no external storage).

Create and initialize a MapView, add it to the Activity 100% width and 100% height.
Enable the zoom and multi-touch controller.
Zoom in to level 14 then set the MapView center to a location (sunny Norfolk, UK!).

I've found that setting the map center and then immediately setting the zoom level does not work as expected.
I think that while the MapView is setting the map center it also zooms in so the end result is unpredictable.

Pan and zoom the map, now rotate your device and you'll see the map returns to it's initial state of zoom level 14, center (52.75192, 0.40505).

I shall show you how to save and restore the MapView state next...

Martin.
 

Attachments

  • 01 - SimpleMap.zip
    5.8 KB · Views: 4,809
Last edited:

Emerito

Active Member
Licensed User
Longtime User
Fact.

I am working in Windows 7. Is there any problem if I upgrade to Windows 10?

Again, thank you.
 

warwound

Expert
Licensed User
Longtime User
Fact.

I am working in Windows 7. Is there any problem if I upgrade to Windows 10?

Again, thank you.


Hmm..
Is this question related to the OSMDroid library?

b4a should work the same regardless of the version of Windows you use.
Though with older versions of Windows it might be necessary to update your .NET library so that the b4a IDE works properly.
 

Emerito

Active Member
Licensed User
Longtime User
I'm working on Windows 7 without any problem.
Now offer me the update to Windows 10 and I would, if I upgrade, trouble with B4A, when I'm in the middle of a job!.
As I use the Microsoft translator, not be if I explain clearly enough.
A greeting
 

warwound

Expert
Licensed User
Longtime User
Well in theory there should be no problem if you upgrade from Windows 7 to Windows 10.
But if you are in the middle of a job i'd certainly complete the current job and then upgrade.
 

Emerito

Active Member
Licensed User
Longtime User
Hi Martin

After clicking on a button (ic_...) ImageButtonsView, I can not return to click on it.
It is disabled. Not resolved it.
 

warwound

Expert
Licensed User
Longtime User
Hi.

I'm not sure what to suggest.
Without seeing your code it's impossible to help.

Can you post a b4a project that shows the problem - i'll compile it and try to debug the issue.

Martin.
 

Emerito

Active Member
Licensed User
Longtime User
FIX.

Thank you Martin, was a silly bug in my code, wore False instead of True

Greetings.
 

Jean Pierre MALOBERTI

Member
Licensed User
Hi all

I'm testing B4A and OSMDroid and I'm impressed by your work guys !
Unfortunately, I'm trying to run some code from version 3.08 in version 4.1 with the same data (a simple ZIP from jpg tiles).

The 308 code was :
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("fond")
   
    ts.Initialize("extrait2", 12, 14, 500, ".jpg", "http://localhost/")
    ts.TMSMode = False
    Carto.Initialize("")
    Carto.SetDataConnectionEnabled(False)
    Carto.AddXYTileSource(ts)
    Carto.SetTileSource("extrait2")
    Carto.SetZoomEnabled(True)
    Carto.SetMultiTouchEnabled(True)
    If FirstTime Then
        Starter.MapCenter.Initialize(-21.005,55.280)
        Starter.ZoomLevel=12
        CompassEnabled = MaPosition.CompassEnabled
        FollowLocationEnabled = MaPosition.FollowLocationEnabled
        MyLocationEnabled = MaPosition.MyLocationEnabled
    Else
        MaPosition.CompassEnabled = CompassEnabled
        MaPosition.FollowLocationEnabled = FollowLocationEnabled
        MaPosition.MyLocationEnabled = MyLocationEnabled
    End If
    Carto.Zoom = Starter.ZoomLevel
       Carto.SetCenter3(Starter.MapCenter)

    PanCarte.AddView(Carto, 0, 0, 100%x, 100%y)
End Sub
All is ok.

The 4.1 code is :
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("fond")
   
    Carto.Initialize("")
    Carto.SetUseDataConnection(True)
   
    Dim tsName As String = "extrait2"
    Dim TileSourceFactory1 As OSMDroid_TileSourceFactory
   
    If Not(TileSourceFactory1.ContainsTileSource(tsName)) Then
        Dim MyTileSource As OSMDroid_UrlTileSource
        MyTileSource.Initialize(Carto, tsName, "http://localhost/%3$d/%1$d/%2$d.jpg", 12, 14, 500)
        TileSourceFactory1.AddTileSource(MyTileSource)
    End If
    Carto.SetTileSource(TileSourceFactory1.GetTileSource("extrait2"))
   
    Carto.SetBuiltInZoomControls(True)
    Carto.SetMultiTouchControls(True)
    If FirstTime Then
        Starter.MapCenter.Initialize(-21.005,55.280)
        Starter.ZoomLevel=17
    End If
    Carto.GetController.SetZoom(Starter.ZoomLevel)
       Carto.GetController.SetCenter(Starter.MapCenter)

    PanCarte.AddView(Carto, 0, 0, 100%x, 100%y)
End Sub

The tiles are no more displayed. What's wrong ?
The mImageFilenameEnding attribute is not initialized. Is it a problem ?

Thanks
 

PCowling

Member
Licensed User
Longtime User
Hi

I have been having a play with the examples here, about half way through the day the map tiles stopped showing on the emulator. I have not changed any code. I thought there was an unlimited tile call for OSM?

I have downloaded and tried fresh projects, emulators and tried different tile sources, so think I have eliminated all the possible options.

Any help or advice would be great thanks.
 

warwound

Expert
Licensed User
Longtime User
The free to use tileservers have a habit of suddenly failing to serve any tiles.
They are generally funded by donations, and have limited resources.
They can become overloaded at any time and then simply fail to respond to requests for tiles.

The solution is to find another tileserver, one that is not so overloaded - that's unlikely to happen as other free tileservers will probably be overloaded too.

You could try the offline map example and use MOBAC to render your own offline tile archive:
https://www.b4x.com/android/forum/threads/mobac-supports-mapsforge-for-offline-tiles.53277/
 

Emerito

Active Member
Licensed User
Longtime User
Hi Martin,

The application I am doing uses the OSMDROID library. Make Tracks and set Waypoints.
They could tell me:
That kind of license do I need.
How to apply for it.
Thank you.
 

warwound

Expert
Licensed User
Longtime User
@Emerito

You want to handle the licenses for both: OSMDroid java library and the map tiles you use in your app.

OSMDroid java library is licensed under the Apache License 2.0.
See 'Code license' on the above link, left hand of page.

The license for the tiles you use in your app depends entirely upon which tileserver you use.
The default tileservers used by OSMDroid (Open Street Map based tileservers) license their tiles under the Creative Commons 3.0 BY-SA license.
See 'Content license' on the above lin, just under 'Code license'.
 

Emerito

Active Member
Licensed User
Longtime User
Excuse my stupidity. I'm having trouble interpreting the translation.
I understand that you attaching the downloaded document in
http://www.apache.org/licenses/LICENSE-2.0 ( TXT or HTML )
adding my data, is it enough? i.e., the license is granted?
In any case. Which would be the B4A tool to show the license?
You beg once more that I apologise.
Thanks

Could I send you the apk to see it?
 

warwound

Expert
Licensed User
Longtime User
@Emerito

I'd suggest you add a new Activity to your project - an 'About' or 'Credits' Activity.
In the new Activity add a WebView and use the WebView to display something like:

This application uses the OSMDroid library which is licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License 2.0</a> license.<br>
<br>
OSMDroid displays map tiles which are licensed under the <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons 3.0 BY-SA</a> license.
 

Emerito

Active Member
Licensed User
Longtime User
Gracias.
Mi duda ahora es: ¿Tengo que solicitar una licencia personal o poniendo lo que me envía es suficiente?
Disculpe mi torpeza.
Saludos.

Thank you.
My question now is: do I have that request a licensed staff or putting what I send is enough?
Excuse my stupidity.
Best regards.
 
Top