Android Tutorial OSMDroid - MapView for B4A tutorial

warwound

Expert
Licensed User
Longtime User
You need to keep a reference to all Marker objects that you create as you need that reference in order to remove the Marker from the map.

The various overlays that you add a Marker to all have a RemoveMarker method, and a RemoveMarkers method too.
So you can remove a single Marker as long as you have a reference to the Marker and pass the reference to the overlay's RemoveMarker method.
And you can remove all Markers from an overlay by calling RemoveMarkers - no reference to the Markers is required with this method.

Martin.
 

davelew1s

Active Member
Licensed User
Longtime User
Hi Martin!
Thanks or the quick reply..you said,,
"You need to keep a reference to all Marker objects that you create as you need that reference in order to remove the Marker from the map."
I'm not 100% sure what that means, here is the sub I use to put markers down:-
Sub Mark_Click
Dim b As Button
b = Sender
Dim value As String
value = b.Text

Select value
Case "Start Drift"
Dim Icon As BitmapDrawable
Icon.Initialize(LoadBitmap(File.DirAssets, "marker_start_icon.png"))
Case "End Drift"
Dim Icon As BitmapDrawable
Icon.Initialize(LoadBitmap(File.DirAssets, "marker_end_icon.png"))
Case "Fish"
Dim Icon As BitmapDrawable
Icon.Initialize(LoadBitmap(File.DirAssets, "marker_fish1_icon.png"))
End Select

markersactive = True
Dim name As String = Marker & markernumber

Marker0 .Initialize3 (name,"Text to gohere",MapView1.GetCenter,Icon)
markernumber = markernumber + 1
markers .Add (Marker0)
MarkersOverlay1.AddMarkers(markers)
MapView1 .Invalidate
lastmark = (MapView1.GetCenter) 'Remember last mark so you can get the distance later

End Sub

is 'Marker0' the reference you mean because all my markers have the same ref.
I tried to do (marker & marknumber) .Initialize3 (nam..... but it didn't work I searched the forum for any help on this but got nothing.
Can you point me in the right direction?
Thanks Dave.
 

warwound

Expert
Licensed User
Longtime User
is 'Marker0' the reference you mean

Yep!

Maybe you could do something like this:

B4X:
Sub Globals
   '   create a Map in your Globals
   Dim MyMarkers As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
   '   Initialize the Map in Create
   MyMarkers.Initialize
End Sub

Sub Mark_Click
   Dim b As Button
   b = Sender
   Dim value As String
   value = b.Text

   Select value
   Case "Start Drift"
   Dim Icon As BitmapDrawable
   Icon.Initialize(LoadBitmap(File.DirAssets, "marker_start_icon.png"))
   Case "End Drift"
   Dim Icon As BitmapDrawable
   Icon.Initialize(LoadBitmap(File.DirAssets, "marker_end_icon.png"))
   Case "Fish"
   Dim Icon As BitmapDrawable
   Icon.Initialize(LoadBitmap(File.DirAssets, "marker_fish1_icon.png"))
   End Select

   markersactive = True
   Dim name As String = Marker & markernumber
   
   '   create a new Marker, keep a reference to it using the Marker name as a key in the MyMarkers Map
   Dim NewMarker As Marker
   NewMarker.Initialize3 (name,"Text to gohere",MapView1.GetCenter,Icon)
   MyMarkers.Put(name, NewMarker)
   
   markernumber = markernumber + 1
   markers .Add (NewMarker)
   MarkersOverlay1.AddMarkers(markers)
   MapView1.Invalidate
   lastmark = (MapView1.GetCenter) 'Remember last mark so you can get the distance later
End Sub

Sub RemoveMarker(Name As String)
   Dim MarkerToRemove As Marker=MyMarkers.Get(Name)
   MarkersOverlay1.RemoveMarker(MarkerToRemove)
End Sub

You can use a Map where the map key is the Marker (unique) name and the Map value is the reference to the Marker that you will later require to remove it from the MarkersOverlay.
(Obviously that's not a complete example, it just shows were in 'Sub Globals' and 'Activity_Create' you'd create and initialize the new MyMarkers object).

Martin.
 

davelew1s

Active Member
Licensed User
Longtime User
Hi Martin!
That works great I've not used a map before so I will have to do some reading to fully understand it.
One final (probably not) question which may be linked with this ... is there a way to save to file and read back to the tablet and PC. I was hoping for a csv file,saving the pathoverlay should be easy but the markers list might not...any thoughts?
Thanks Dave.
 

warwound

Expert
Licensed User
Longtime User
You might be able to use the RandomAccessFile library and it's WriteObject and ReadObject methods to save and load a PathOverlay and a Map containing Markers.

I have successfully used RandomAccessFile to save and load single GeoPoint objects and think it will also work with a Map of Markers though am not sure about a PathOverlay.
PathOverlay has a GetAllPoints method that returns a List of GeoPoints - so if saving and loading a PathOverlay fails next try saving and loading this List of GeoPoints.

Within the OSMDroid library java these OSMDroid objects such as GeoPoint implement a java interface that enables them to be serialized and therefore saved and loaded to and from file.
But if the RandomAccessFile fails to work then parsing all your PathOverlay GeoPoints and Marker properties to a CSV file is an alternative solution - not a very elegant solution.
A more elegant solution would be to use the SQL library to save all of that data, it'd be a more suitable tool for the job.
SQL tutorial can be found here: http://www.b4x.com/forum/basic4android-getting-started-tutorials/6736-sql-tutorial.html.

Martin.
 

davelew1s

Active Member
Licensed User
Longtime User
OK Martin I'll play around with you suggestions and see if I can make it work.
Once again thanks for all your work and your help.
Dave.
 

davelew1s

Active Member
Licensed User
Longtime User
Hi!
I've just discovered the beauty of the MyLocationOverlay and wondered if the following can be changed from true to false?

'FollowLocationAutoDisabled is generated if FollowLocation is enabled and the user drags the map. The drag causes FollowLocation to be automatically disabled.
This is the default behaviour of the native Android OSMDroid library - i couldn't decide whether to change that default behaviour so have left it as it is for now.'
Thanks Dave.
 

warwound

Expert
Licensed User
Longtime User

The best solution is to listen for the FollowLocationAutoDisabled event and re-enable FollowLocationEnabled if it is disabled when the user drags the map.

Martin.
 

ValDog

Active Member
Licensed User
Longtime User
Wow, this library effort is incredible. I've worked through all the tutorial programs and read through all the related forum discussion, and am still trying to get my head around all of it. I had been working with the WebView methodology, but found that limiting for a number of reasons. Your library seems ideal for my needs, but I need to tie together all the overlay elements to accomplish what I am trying to do. Here's a basic description of my program needs:

- load a map, say using the Mapnik source
- listen for changes in GPS location; display a marker for each new location and draw the connecting path between the markers
- display (balloon?) information for a marker when it it touched.

I sure would appreciate a basic roadmap to accomplish this...
 

javiers

Active Member
Licensed User
Longtime User


Hello, how can load layers of openseamaps weather?
 

warwound

Expert
Licensed User
Longtime User
Have you tried to modify the example that displays the openseamap 'seamarks' tiles?
Have you got a working URL to the weather tiles so you can replace the seamarks URL with the weather URL?

Martin.
 

davelew1s

Active Member
Licensed User
Longtime User
Hi!
While we are on a nautical theme, I've written a small app for use when I go out fishing...it seems to be working OK...so now's the time to mess it up.
Has anyone found and loaded nautical charts? I've used openseamaps whick is OK but I was thinking more like Garmin Bluechart..charts with all nautical info.
Dave.
 

javiers

Active Member
Licensed User
Longtime User


Fixed. The correct URL is:

TilesOverlay2.Initialize ("SeaMap", 4, 7, 256, ".Png", "http://www.openportguide.org/tiles/actual/wind_vector/5/")

where 5 is the value corresponding to:

the forcast time is following with "t /". Actually "t" Can Have the Following values: 5, 7, 9, 11, 15, 19, 23 or 27. A more detailed description for "t":
 

davelew1s

Active Member
Licensed User
Longtime User
Hi javiers!
Not quite what I looking for but very interesting...got it working the same as you, will probabaly use it later in another project.
Thanks Dave.
 

javiers

Active Member
Licensed User
Longtime User
It's a way to get weather forecasts for interventions of emergency services (forest fires, floods ...).

This community and the developers are awesome. A greeting.
 

warwound

Expert
Licensed User
Longtime User
Hello, is possible move a marker on the map?

Nope!

A marker is in fact an OverlayItem (using the java terminology) and if you take a look at the documentation: http://code.google.com/p/osmdroid/s...a/org/osmdroid/views/overlay/OverlayItem.java you'll see it states:

Immutable class describing a GeoPoint with a Title and a Description.

The only solution is to remove a marker and then create a new one and add it to the map when you want to change a marker position.

Martin.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…