B4A Library OSMDroid - MapView for B4A

Here we have my latest library - OSMDroid provides a MapView for B4A.

More info on the original (native Android) OSMDroid project can be found here: osmdroid - OpenStreetMap-Tools for Android - Google Project Hosting.

Library reference is no longer included in this post due to limits on the number of characters allowed in a single post.

I have created some tutorials to show basic usage of the library and will update this thread with a link to them as soon as i have them all uploaded.

** Your attention is drawn to the included file Apache License Version 2.0.txt, which is a copy of the Apache License Version 2.0 under which the native Android OSMDroid library is released **

Martin.
 

Attachments

  • OSMDroid_3_0_8_v3.60.zip
    361.9 KB · Views: 4,228
Last edited:

warwound

Expert
Licensed User
Longtime User
Well i took the MarkersOverlay demo project, updated it's Click and LongClick event handling subs so they both returned True and gave it a test.
It works as expected on an old HTC Desire S running android 4.2.2.
Also tested and working as expected on an old ZTE Blade running android 2.3.7.

Does the attached project work as expected for you?
What device and android version are you finding both events fired on?

Martin.
 

Attachments

  • OSMDroid-MarkersOverlay-ClickTest.zip
    8.1 KB · Views: 318

canalrun

Well-Known Member
Licensed User
Longtime User
Well i took the MarkersOverlay demo project, updated it's Click and LongClick event handling subs so they both returned True and gave it a test.

Does the attached project work as expected for you?

Martin.

Thanks. It just keeps getting stranger and stranger…

I misspoke before. If I do a MarkerOverlay_LongClick I also get a TouchEventsOverlay_LongClick, not a MarkerOverlay_Click as I had previously stated.

The project you attached works correctly in my testing with the latest "GetBoundingBox" (3.54) library.

I also modified your project a little bit to make it look more like my code. I added a touch overlay and moved the AddOverlay methods to add the scale bar,
touch overlay, followed by the marker overlay – in that order. In the marker overlay long click I added a message box. I removed the MiniMap.

With my modifications, it also works.

I took a project from about a year ago using an older library (3.4, I think). The APK built about a year ago works as expected – no touch overlay longclick when I do a marker overlay longclick.

I compiled this project using the newer, 3.54, library. Now it doesn't work – I get a touch overlay long click when I do a marker overlay longclick, but I did not modify the code at all so the callback events don't return a Boolean.

I can't imagine what's going on in my code. It's basically the same as the attached project as far as OSMDroid is concerned, but of course with a bunch of other stuff added.

Any ideas what it may be. I'm thinking I may not be returning a Boolean, but every return path looks correct. I have a button with the activity as a parent that's in front of the map and overlays. Could that be causing the problem? I will try removing that.

One solution would be for me to use a flag to disable touch overlay long clicks within 500 ms or so of a marker overlay long click – but I'd rather find the real problem.

Barry.
 

Attachments

  • MyOverlaysMap.zip
    8.1 KB · Views: 276
Last edited:

warwound

Expert
Licensed User
Longtime User
A b4a Sub by default returns an empty String.
If a Sub does not define a return value it will return an empty String.

So if the click and long click callback Subs do not explicitly return a Boolean then they will in practice return an empty String to the library.
The library code looks at the value returned from the b4a Sub - if it is a Boolean then the Boolean True or False value indicates whether the b4a Sub has handled the event.
If the returned value is not a Boolean (it's an empty String) then the library will assume a value of False has been returned.
So if your old code does not explicitly return a Boolean then the library will function as if a Boolean False has been returned - the touch event will propagate to the next overlay.

Does that make sense?

Martin.
 

canalrun

Well-Known Member
Licensed User
Longtime User
A b4a Sub by default returns an empty String.
If a Sub does not define a return value it will return an empty String.

So if the click and long click callback Subs do not explicitly return a Boolean then they will in practice return an empty String to the library.
The library code looks at the value returned from the b4a Sub - if it is a Boolean then the Boolean True or False value indicates whether the b4a Sub has handled the event. ...

Does that make sense?

Martin.

Yep.

Oooh, a lightbulb just went on. This explains why my old code didn't work – as expected. But, in my new project I return a true. Your statement, "If a Sub does not define a return value". I bet I forgot to add the "As Boolean" to the subroutine declaration – this will probably solve the issue.

Added Later:
Adding the "As Boolean" to the sub declarations did solve the issue.

"Sub Tch_ovl_LongClick(gp As GeoPoint) As Boolean"

Thanks,
Barry.
 
Last edited:

androidvh

Member
Licensed User
Longtime User
Hallo Martin,

comming up two questions:

1. is it possible to get a zoom > 18 (using Mapnik, working local)

2. is it possible to load an icon by runtime from your AndroidResources (\Objects\res\drawable-nodpi)


Wish You and all other people a happy new year...


Kind regards Volker
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks for the 3.54 update to the library. Everything works well.

I noticed that sometimes tiles that are available don't display on the map. I notice this mostly when I'm using off-line data.

For example I will download off-line data, put the phone in flight mode (to test no network connection), and then scroll the map to the area I just downloaded. As I move the map, tiles that should be there display as a blank area. I have tried mapview.invalidate – this does not show the missing tiles. But, if I do an orientation change (rotate the phone), the area with missing tiles now shows. I rotate the phone back to the original position and the tiles are still there.

Doing the orientation change goes through activity create and re-initializes the mapview. This must internally trigger a re-search for tiles that is not done during an invalidate.

Telling the user to rotate the phone is not a bad solution, but it would be nice to be able to have the user click the screen to redraw the map looking for any missing tiles.

Is that possible? Is there something like a map refresh or super-invalidate that would force tiles to be re-searched for?

Thanks,
Barry.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
I'm sure this is a bug with OSMDroid.
This morning i've created an OSMDroid map which displays standard Mapnik tiles and also displays a TileOverlay.
The TileOverlay tiles are rendered on the fly on the device.

The map tends to display just the Mapnik tiles until you pan or zoom it and then it displays the TileOverlay.

Try setting the map center to the map center and see if that forces a redraw:

B4X:
MapView1.SetCenter3(MapView1.GetCenter)

Martin.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks,

The SetCenter3 did not seem to force a refresh and redraw of tiles.

I am currently not using a TilesOverlay. What is the function of this? It is only briefly described in the Help file I have for your OSMDroid. I will have to search for one of your demos that uses this function.

I see the Initialize method takes a BaseURL, also a TileFilenameEnding. I guess this would work with no network connection (Flight Mode)?

Previously you've mentioned the possibility of an upgraded OSMDroid – maybe some time this year. Having to rotate the phone is really not a bad solution.

Barry.
 

warwound

Expert
Licensed User
Longtime User
Thanks,

The SetCenter3 did not seem to force a refresh and redraw of tiles.

I am currently not using a TilesOverlay. What is the function of this? It is only briefly described in the Help file I have for your OSMDroid. I will have to search for one of your demos that uses this function.

I see the Initialize method takes a BaseURL, also a TileFilenameEnding. I guess this would work with no network connection (Flight Mode)?

Previously you've mentioned the possibility of an upgraded OSMDroid – maybe some time this year. Having to rotate the phone is really not a bad solution.

Barry.

I posted some info about XYTileSource and TilesOverlay earlier:
http://www.b4x.com/android/forum/threads/osmdroid-mapview-for-b4a-tutorial.16310/page-13#post-212926

If you have cached tiles on the device and network connection is disabled then OSMDroid should still load any available cached tiles.

Martin.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks.

I am exploring a different path. I think what may have happened is that my app downloadrd new tiles, put them in the osmdroid directory in a zip file, but OSMDroid did not recognize they exist until the mapview was re-initialized.

This would make sense and speed things up in terms of map processing for OSMDroid.

It was just a coincidence that my previous test tried to use tiles that had just been downloaded without re-initializing the map view.

Re-initializing the map view after tile download would alleviate this issue.

Barry.
 

canalrun

Well-Known Member
Licensed User
Longtime User
Any help? where I find the NativeOSMDroid.jar file????

Hello,
If I understand your question and remember correctly, I think when the OSMDroid was first released it was released under the name NativeOSMDroid.jar. The name was changed to just OSMDroid.jar.

I use the version of the library that warwound released on December 10 (see post 239 of this thread).

Hope this is correct.

Barry.
 

wizard699

Active Member
Licensed User
Longtime User
Thanks canalrun, but warwound in tutorial says

"This library is OSMDroid.jar and OSMDroid.xml PLUS two additional (native Android) libraries NativeOSMDroid.jar and slf4j-android-1.5.8.jar.
Those last two libraries are not in the B4A library download, you can get them from the main library thread, native_android_libraries.zip is the archive to download."

Then, NativeOSMDroid.jar are another library to add to folder library.
 

wizard699

Active Member
Licensed User
Longtime User
Do you have this two file

NativeOSMDroid.jar
slf4j-android-1.5.8.jar

in you libraries folder?
 

canalrun

Well-Known Member
Licensed User
Longtime User
Nope.

Neither of those two files is in the B4A Libraries or AdLibs folder. In fact, I searched the entire computer using Agent Ransack – those two files are not present anywhere on the computer.

For OSMDroid the library I use is: OSMDroid_3_0_8.jar with its associated XML file. Everything is working very well.

Barry.
 

warwound

Expert
Licensed User
Longtime User
Hallo Martin,

comming up two questions:

1. is it possible to get a zoom > 18 (using Mapnik, working local)

How are these tiles stored - are they in a zip tile archive?
How do you display them in OSMDroid? Do you simply choose a TileSource that corresponds to the local tiles?

2. is it possible to load an icon by runtime from your AndroidResources (\Objects\res\drawable-nodpi)

Yes that's possible, take a look here:
http://www.b4x.com/android/forum/threads/osmdroid-mapview-for-b4a.16309/page-3#post-115572.

Martin.
 

androidvh

Member
Licensed User
Longtime User
Hallo Martin,

I store my tiles in folders(osmdroid/tiles/Mapnik) up to zoom Level 18, not in a zip Archive.
I don´t choose a TileSource
settings : Mapview1.SetDataConnectionEnabled(False)

So what is to do for zoom > 18

Volker...
 

warwound

Expert
Licensed User
Longtime User
Hallo Martin,

I store my tiles in folders(osmdroid/tiles/Mapnik) up to zoom Level 18, not in a zip Archive.
I don´t choose a TileSource
settings : Mapview1.SetDataConnectionEnabled(False)

So what is to do for zoom > 18

Volker...

The problem here is that you're using a built in TileSource and simply providing 'already cached' images for that TileSource.
And the built in Mapnik TileSource is hardcoded to have a maximum zoom level of 18.
You cannot change that hardcoded value.

Maybe you could create an XYTileSource with a higher maximum zoom level...
The example code in the linked to thread could be modified:

B4X:
MyXYTileSource.Initialize("MapnikOffline", 19, 22, 256, ".jpg", "http://localhost/")

You'd now have a new TileSource named MapnikOffline with zoom levels 19 to 22 available, all HTTP requests for tiles would fail as they'd request tiles from localhost.
But before requesting tiles over HTTP the new XYTileSource would check for the existence of cached tiles at osmdroid/tiles/MapnikOffline.
So put your offline tiles in that folder instead of the default osmdroid/tiles/Mapnik folder.

Martin.
 
Top