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:

Spinter

Active Member
Licensed User
Longtime User
I have not solved!
For example, if I have to read the title of a marker and placed under another marker can not do it. if I click on a google map for the second time on the marker I select one below!
 

warwound

Expert
Licensed User
Longtime User
Sounds like you need a marker clustering solution.
Last week i started to wrap the OSMBonusPack library.
One of it's features is a marker clustering solution: http://code.google.com/p/osmbonuspack/wiki/Tutorial_3.

I've no idea when i will work more on the OSMBonusPack library - i have a busy week ahead working on another project.

Does the OSMBonusPack clustering solution look like it'd solve your problem?

Martin.
 

Spectre

Active Member
Licensed User
Longtime User
slf4j-android-1.6.1-rc1.jar can be found in the archive you presumably downloaded a few days back?

http://b4a.martinpearman.co.uk/osmdroid/MultiViewHost-20140323.zip

You need the updated library files from http://b4a.martinpearman.co.uk/osmdroid/OSMDroid_v4.1-20140329.zip and slf4j-android-1.6.1-rc1.jar from http://b4a.martinpearman.co.uk/osmdroid/MultiViewHost-20140323.zip.
Do not use the now out of date OSMDroid_4_1.jar and OSMDroid_4_1.xml files from http://b4a.martinpearman.co.uk/osmdroid/MultiViewHost-20140323.zip.

Martin.

Hi!
you can have a single zip file with the updated libraries? Every time I try to download a zip file I get stuck chrome explorer etc ...
 

warwound

Expert
Licensed User
Longtime User

Spectre

Active Member
Licensed User
Longtime User
Hi Martin, very good job...
I started to change my project in your new version. I find a lot of differences. Do you have any examples or documentation in order to proceed with examples?
For example, the GPS tells me more event:

B4X:
MyLocationOverlay1_LocationChanged (newLocation As Location, IsFirstFix As Boolean)
 

warwound

Expert
Licensed User
Longtime User
http://b4a.martinpearman.co.uk/osmdroid/OSMDroid_v4.1_library_files_20140331.zip

I've updated the MyLocationOverlay.
It's Initialize method now requires an EventName parameter:

Initialize (EventName As String, MapView1 As MapView)

And it raises the event:

LocationChanged (NewLocation As Location, IsFirstFix As Boolean)

The version 4.1 MyLocationOverlay doesn't raise the other events that the version 3.0.8 MyLocationOverlay raises.
The ProviderChanged event has been moved to another class which i have not yet wrapped.

Do you use either of the ProviderChanged or FollowLocationAutoDisabled events?

There's little help available converting from 3.0.8 to 4.1, all i can say is to post a list of any missing methods, events or properties and i'll take a look.

Martin.
 

Spinter

Active Member
Licensed User
Longtime User
Sounds like you need a marker clustering solution.
Last week i started to wrap the OSMBonusPack library.
One of it's features is a marker clustering solution: http://code.google.com/p/osmbonuspack/wiki/Tutorial_3.

I've no idea when i will work more on the OSMBonusPack library - i have a busy week ahead working on another project.

Does the OSMBonusPack clustering solution look like it'd solve your problem?

Martin.


I would need a sent to back marker superimposed with similar cordinate
 

Spectre

Active Member
Licensed User
Longtime User
http://b4a.martinpearman.co.uk/osmdroid/OSMDroid_v4.1_library_files_20140331.zip

I've updated the MyLocationOverlay.
It's Initialize method now requires an EventName parameter:

Initialize (EventName As String, MapView1 As MapView)

And it raises the event:

LocationChanged (NewLocation As Location, IsFirstFix As Boolean)

The version 4.1 MyLocationOverlay doesn't raise the other events that the version 3.0.8 MyLocationOverlay raises.
The ProviderChanged event has been moved to another class which i have not yet wrapped.

Do you use either of the ProviderChanged or FollowLocationAutoDisabled events?

There's little help available converting from 3.0.8 to 4.1, all i can say is to post a list of any missing methods, events or properties and i'll take a look.

Martin.

I solved was the statement that was wrong and did not generate event ... Markers etc.. seem to work even if I can not show the label with the description always visible. Even the orientation of the map is wrong, see code and error ... Best regards.



B4X:
Sub Sensor_SensorChanged (Values() As Float)
Dim ZeroOrientation As Float
ZeroOrientation=01.0
RotateCompass(Round2(Values(0), 1))
lblMagBearing.Text=(Round2(Values(0), 0))
LocalMagBearing=(Round2(Values(0), 0))'transport mag bearing
lblMag.text=(Round2(Values(0), 0))
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    If TickMapOrientation=2 Then
        Select Case Global.Glob_MAPORIENTATION       
        Case 0
            'MapView1.GetMapOrientation=ZeroOrientation
            TickMapOrientation=0
        Case 1           
            MapView1.GetMapOrientation=Values(0)' direction  head
            TickMapOrientation=0
        Case 2
            MapView1.GetMapOrientation=(Values(0))+ (ActualTargetBearing) 'WP direction
            TickMapOrientation=0
        End Select
    End If   
End Sub

Error:
Parsing code. 0.23
Compiling code. 0.36
Compiling layouts code. 0.02
Generating R file. 0.07
Compiling generated Java code. Error
B4A line: 940
MapView1.GetMapOrientation=ZeroOrientation'heading direction
javac 1.7.0_51
src\Combat\NAVI\map_view.java:5361: error: unexpected type
mostCurrent._mapview1.GetMapOrientation() = _zeroorientation;
^
required: variable
found: value
1 error
 

Spectre

Active Member
Licensed User
Longtime User
Hi!
has also changed the system to set the tile source? MapView1.SetTileSource ("CycleMap")
 

Spinter

Active Member
Licensed User
Longtime User
MapView1.SetTileSource(TileSourceFactory1.GetTileSource(TileSource))
Hi!
has also changed the system to set the tile source? MapView1.SetTileSource ("CycleMap")

DimTileSourceFactory1AsOSMDroid_TileSourceFactory
MapView1.SetTileSource(TileSourceFactory1.GetTileSource("CycleMap"))
 

Spectre

Active Member
Licensed User
Longtime User
tanks... a little difficult without help ...
MapView1.SetTileSource(TileSourceFactory1.GetTileSource(TileSource))


DimTileSourceFactory1AsOSMDroid_TileSourceFactory
MapView1.SetTileSource(TileSourceFactory1.GetTileSource("CycleMap"))
 

Spectre

Active Member
Licensed User
Longtime User
I've just uploaded a couple more example projects for the new version 4.1 library: http://b4a.martinpearman.co.uk/osmdroid/OSMDroid_v4.1_more_examples.zip.

One shows use of the 'classic' balloon infowindow to show Marker data when a Marker is clicked.

The other is similar but uses a plain b4a Label to show Marker data when a Marker is clicked.
This second example may be of interest to you.
If each of you Markers has an associated Label you could update the Label Text when the Marker is clicked for example.

Martin.

HI! I can not download this example I wanted to see ...I wanted to try to see the label on the map ...
 

Spectre

Active Member
Licensed User
Longtime User

Spectre

Active Member
Licensed User
Longtime User
Hi!
In the latest versions of Marker example 20140227 You can assign a different icon for each marker?
tanks spectre...
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
regards
that could be the following errors, I tried but I can not seem to fix it
thanks
the error is :
java.lang.NullPointerException
 

Attachments

  • error.PNG
    error.PNG
    7.7 KB · Views: 343
Top