Android Tutorial OSMDroid - MapView for B4A tutorial

You can find the OSMDroid library thread here: http://www.basic4ppc.com/forum/addi...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,103
Last edited:

DonManfred

Expert
Licensed User
but i want to try offline map.where i can download offline map for OSM Mapnik?
Use MOBile Atlas Creator (like described in Post #1)

Start the tool, select a working tilesource, slect a small area and define which zoomlevel you want to have, add the selected area to the Atlascontent... Create Atlas

mobac0007.png
 

supriono

Member
Licensed User
Use MOBile Atlas Creator (like described in Post #1)

Start the tool, select a working tilesource, slect a small area and define which zoomlevel you want to have, add the selected area to the Atlascontent... Create Atlas

mobac0007.png
thanks ..
but i don't see map...image only cros X
 

androidvh

Member
Licensed User
Dear Martin,

I use your libary in version 4.1.
Since yesterday no tiles are loaded.
You can check it if you rename the osm folder.

Any idea?

Kind regards Volker
 

cas6678

Active Member
Licensed User
Hello

¿Is there any tutorial to work with offline maps?

I want to know how to generate a map file to work offlline, where to locate and how to make this library you use this new map.

Thank you.
 

warwound

Expert
Licensed User
Hello

¿Is there any tutorial to work with offline maps?

I want to know how to generate a map file to work offlline, where to locate and how to make this library you use this new map.
Are you talking about creating a mapsforge .map file?

If so then look here for general documentation:
https://github.com/mapsforge/mapsforge/tree/master/docs

And here in particular for documentation about the .map file creation:
https://github.com/mapsforge/mapsforge/blob/master/docs/Getting-Started-Map-Writer.md

I can't find a forum link to the tutorial on using a .map file in OSMDroid but i'm sure if you search for 'mapsforge' you'll find it.
 

cas6678

Active Member
Licensed User
Hello @warwound

I don`t know mapsforge. Now I´m read your links.

Maybe I´ve problems with the translation from Spanish, sorry.

I tried your library and works very well, thank you.

But I want to use offline, the questions are:

It's possible?

That software to create the map?

Where I place the map to your library can use it?

Maybe some example b4a for maps offline whit you library?

Thanks.
 

Medexpert

Member
Licensed User
Dear All,

I didn't found solution for street font size on high dpi display. I use Samsung s6 and street name on map is too small in the bigest zoom view too.
The library is OSMDroid 3.08.
Could you help me what is the solution?

Thanks in andvanced,
Istvan
 
Last edited:

maso1975

Member
Licensed User
Hello everyone, after searching a lot in this forum and on the internet, what I have left is to ask. In advance I thank anyone who takes five minutes to answer and my question is the following:

Why, according to the terminal model where I install an application with a mapview and omsdroid, the map is visualized with different scales?

For example, if I use a Galaxy Core 2, the map has a maximum zoom scale of 113m and if it is a Galaxy S3 the scale is 160m or 120m I do not remember well.

On the other hand and I approve to ask, as I could do an additional zoom to the map, ie, I need to be able to zoom up to at least 10m according to the scale, it is possible, since I have seen applications with the same titles that I am Using which I downloaded with Mobile Atlas.

Somebody could help me.

Thanks again.
 

maso1975

Member
Licensed User
Hello Medexpert, from what I am seeing, your problem is similar to mine, that is, from what I understand, the scale of the map in the S6 is very small, the same is true with my S6, when I execute the entire map application It looks very small, if I do in a Core 2 for example, it looks a little bigger, all this in the maximum zoom and I can not find the solution.

Greetings.
 

maso1975

Member
Licensed User
Hello, I think the problem of map scale is a limitation of the library, after searching and searching without stopping, I found here: https://github.com/osmdroid/osmdroid/issues/81, a solution that raises Kurtzmarc:

To fix # 49, we would also like to add something like this to MapView:

Public void setTileSource (final ITileSource aTileSource) {
MTileProvider.setTileSource (aTileSource);
Float density = getResources (). GetDisplayMetrics (). Density;
TileSystem.setTileSize ((int) (aTileSource.getTileSizePixels () * density));
This.checkZoomButtons ();This.setZoomLevel (mZoomLevel);// revalidate zoom level
PostInvalidate ();​
}

And here: http://stackoverflow.com/questions/7240568/osmdroid-display-tiles-bigger, another that poses this solution:


final float scale = getBaseContext().getResources().getDisplayMetrics().density;
final int newScale =(int)(256* scale);
String[]OSMSource=newString[2];
OSMSource[0]="http://a.tile.openstreetmap.org/";
OSMSource[1]="http://b.tile.openstreetmap.org/";
XYTileSourceMapSource=newXYTileSource("OSM",null,1,18,newScale,".png",OSMSource);
map.setTileSource(MapSource);​

Of course this is in JAVA.

Anyone know if you could bring this code to B4A, or use the JAVA embedded in the B4A code to use any of these solutions?
 

tracklocator

Member
Licensed User
Very good work @warwound has helped me a lot to get tiles from two sources or more is documented in one of the threads and I do with version 4.1 and works well

B4X:
MyTileSource.Initialize2(MapView1, MyTileSourceName, Array As String ("URLservertiles.png", "URLserver.tiles1.png"), 0, 18, 256)
            TileSourceFactory1.AddTileSource(MyTileSource)
 

cas6678

Active Member
Licensed User
Hi all.

After a lot of testing I have the working example with the OSDDroid 3.08 and OSMDroid 3.08 MapsForgeTilesSource libraries.

Now I want to add the rotation of the map and I have read that I need to use the library 4.1

But I do not find the new methods and properties equivalent to 3.08 for 4.1. I need These:

MapView OSMDroid_MapView
Methods:
AddOverlay
(anOverlay AsOverlay)
AddTileSource (TileSource1 AsITileSource)
FitMapToBoundingBox (BoundingBox AsBoundingBoxE6)
GetCenter AsGeoPointWrapper GetMapCenter AsGeoPoint ???
GetTileSources AsList
SetCenter3
(aGeoPoint AsGeoPoint)
SetMultiTouchEnabled (Enabled AsBoolean)
SetTileSource (TileSource AsString)
SetZoomEnabled (Enabled AsBoolean)
Properties:
Zoom AsInt
MyLocationOverlay
OSMDroid_MyLocationOverlay
Properties:
CompassEnabled AsBoolean Enabled AsBoolean ???
FollowLocationEnabled AsBoolean Enabled AsBoolean ???
MyLocationEnabled AsBoolean Enabled AsBoolean ???

Thanks
 
Top