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,794
Last edited:

Dario126

Member
Licensed User
Longtime User
So, I presume that You abandoned this project, or Your Win 8 giving You a lot of trouble :=)

Just kidding the second part, but would You recommend to give up and go with google map engine instead of this?
 

ValDog

Active Member
Licensed User
Longtime User
So, I presume that You abandoned this project, or Your Win 8 giving You a lot of trouble :=)

Just kidding the second part, but would You recommend to give up and go with google map engine instead of this?


Not necessarily. I got it working the way I needed to (online and offline) and then started looking at Google Maps. Decided for my project to go with Google Maps (online) and OSMDroid for offline mapping. While I think I liked the OSMDroid functionality better Google Map has some features that are pretty cool - like satellite and hybrid views...
 

peacemaker

Expert
Licensed User
Longtime User
>MarkersFocusOverlay generates no events

It's a pity... :-(
It would be good to show Marker's baloon info, and _also_ next open some popup menu according to the MarkersOverlay1_Click...
Possible to add ?
 

warwound

Expert
Licensed User
Longtime User
@peacemaker

Can you test the attached update?

The MarkersFocusOverlay should now raise these three events:

MarkersFocusOverlay
Events:

  • Focus (Marker1 As Marker)
    InfoWindow shown for Marker1.
  • LongPress (Marker1 As Marker)
    Long press detected on Marker1.
  • UnFocus (Marker1 As Marker)
    InfoWindow no longer shown for Marker1.

Martin.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
I use several overlays: Markers, MarkersFocus, Touch...

These new subs does not triggered for the updated lib, but the marker baloons are shown OK:

B4X:
Sub MarkersFocusOverlay1_Focus(Mark As Marker) As Boolean
ToastMessageShow("Focus = " & Mark.Description, False)
Return True
End Sub

Sub MarkersFocusOverlay1_LongPress (Mark As Marker) As Boolean
ToastMessageShow("LongPress = " & Mark.Description, False)
Return True
End Sub

Sub MarkersFocusOverlay1_UnFocus (Mark As Marker) As Boolean
ToastMessageShow("UnFocus = " & Mark.Description, False)
Return True
End Sub

I mean, the main feature needed - not to consume the events, and allow to work together with other overlays during a user tapps the Markers - but showing the balloon as usual.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Just noticed a bug in the code that raises the long press event - the bug would have prevented that event from being raised.
The code for the focus and unfocus looks ok though.

I've added some logging statements to all three events now, you should see something like markersfocusoverlay1_focus being logged for example.

I'm assuming(!) that you noticed you need to use the new Initialize2 method and pass an EventName and not use the Initialize method?

Modified files attached.

Martin.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Oh, yes indeed, Init is required 2nd !
Now works OK, all 3 events, in log also visible. But UnFocus is rare, strange, i cannot understand when it fires, tested only with 2 markers.
So, it can be used now, thanks!

Strange that i cannot get MarkersOverlay1_Click after Focus.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Strange that i cannot get MarkersOverlay1_Click after Focus.

The MarkersFocusOverlay consumes the click event - so that prevents any Overlay that is underneath the MarkersFocusOverlay from receiving that click.
I need to update the code again to remove those log statements - i'll see if i can update the three new events so that they return a Boolean that indicates whether to consume the event or not.

Martin.
 

peacemaker

Expert
Licensed User
Longtime User
OK, release, please, without logs.
UnFocus now is fired if tap the same Marker secondly :)
 

warwound

Expert
Licensed User
Longtime User
Here's (hopefully) the last update that i want to ask you to test.

MarkersFocusOverlay now raises the same three events but the subs that handles the events must return a Boolean value to indicate whether the event has been consumed or not:

MarkersFocusOverlay
Events:

  • Focus (Marker1 As Marker) As Boolean
    InfoWindow shown for Marker1.
  • LongPress (Marker1 As Marker) As Boolean
    Long press detected on Marker1.
  • UnFocus (Marker1 As Marker) As Boolean
    InfoWindow no longer shown for Marker1.

Martin.
 

Attachments

  • MarkersFocusOverlay-20140221b.zip
    343.8 KB · Views: 504

peacemaker

Expert
Licensed User
Longtime User
UnFocus - the same, occured only when secondly tap the same Marker (is it planned so ?).
Boolean for these events, seems to be unusable, as no such events in other everlays to passthrough?
_Click would be more useful, i guess... but it's usable now since possible to move the code from MarkerOverlay_Click to MarkersFocusOverlay1_Focus now
 

MikeH

Well-Known Member
Licensed User
Longtime User
Martin, Is it possible to have round corners on a mapview?

Cheers,
Mike.
 

MikeH

Well-Known Member
Licensed User
Longtime User
Thanks Martin. I had seen that thread before I asked the question and it showed me how to do rounded corners on my other panels but not the actual mapview. Is it possible to have rounded corners on a mapview without overlaying a ninepatch or other image? If I do go that route, when I zoom the map, I imagine the corners would move inwards when I zoom in, whereas I want the corners to stay where they are.
 

warwound

Expert
Licensed User
Longtime User
@MikeH

To be honest i don't know!
I'd not think any rounded corner would zoom with the map as these rounded corners are not added to the MapView.

I think trial and error is your only way forward.

Martin.
 

Spectre

Active Member
Licensed User
Longtime User
Hi!
I am reading this wonderful post ... I do not know if you can view it on the map markers with the given name?

Spectre...
 

warwound

Expert
Licensed User
Longtime User
Hi!
I am reading this wonderful post ... I do not know if you can view it on the map markers with the given name?

Spectre...

I'm not sure i understand what you are asking...
Can you be a bit more detailed with your question?

Martin.
 

Spectre

Active Member
Licensed User
Longtime User
excuse the English ...
it is possible that in addition to 'icon is always visible on the map the name of the WP that I put in the description?
I also have a problem with compass and scale bar do not see ...

Dim Marker2 As Marker
Marker2.Initialize("Elsewhere", "Downham Market", 52.60801, 0.39047, Null)
Markers.Add(Marker2)

I see on the map icon def.
You can see the description and icon " Elsewhere "
do not always click on the point?
 

warwound

Expert
Licensed User
Longtime User
@Spectre

Permanently displaying an infowindow for more than one Marker is not possible with the current version of the OSMDroid library.
It is though possible with the (not yet complete or upload) new version 4.1.

Version 4.1 would allow you to either:
  • Display one or more infowindows that remain open.
  • Display one or more Labels on the map, each Label anchored to a Marker's position.

The attached b4a project and alpha version of OSMDroid 4.1 shows how to display Labels anchored to Marker positions.
[edit]The demo is too large to attach, you can download it from here: http://b4a.martinpearman.co.uk/osmdroid/MultiViewHost-20140323.zip[/edit]

Version 4.1 syntax differs from the syntax of version 3.0.8, and this alpha version is just that - an alpha where i might and probably will make further changes.

To be honest if could get some forum members to test the new version i'd probably have it completed and ready for production use sooner rather than later.

Problems with the CompassOverlay and ScaleBarOverlay tend to be:
  • Hardware acceleration is not configured in the manifest.
    Try setting hardwareAcceleration to False.
  • The CompassOverlay and ScaleBarOverlay need to be added to the MapView in the right order - adding them in a different order causes them not to display.
    Experiment adding these Overlays to the MapView in a different order after you have added all other Overlays.

Martin.
 

Spectre

Active Member
Licensed User
Longtime User
Hi!
Thanks for the reply ... I'm making an app with labels and icons are essential for good results!
I'll check this new alpha.
I did this test for the compass: I open a new project with your example compass and rudder and it works! Gate from my program lines of the map and I copy and paste the code in your program. Also in this case I do not see anything!!
 
Top