Android Tutorial OSMDroid - MapView for B4A tutorial

Discussion in 'Tutorials & Examples' started by warwound, Mar 26, 2012.

  1. PABLO2013

    PABLO2013 Active Member Licensed User

    HI, MARTIN
    I NOT KNOW THE WAY OR FORM TO PUT THIS IN MY APPLICATION ... AS I DO

    private void setHardwareAccelerationOff() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    myMapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    TKS
     
  2. warwound

    warwound Expert Licensed User

    You'd have to convert that java code to b4a in order to use it.
    Far easier to enable and disable hardware acceleration in the manifest.

    Search the forum and you'll find examples setting hardware acceleration in the manifest.

    Martin.
     
    TAK likes this.
  3. agus mulyana

    agus mulyana Member Licensed User

    Dear all
    execuse me, i found an error after i compilled an examples of OSMDROID, (suddenly occured), here, i attached the print screen of that error :

    http://postimg.org/image/7lehimbdn/

    if you never mind, please hepl me, thank you

    regards
     
  4. TAK

    TAK Member Licensed User

    Hi Martin,
    is it possible to save the path where i move? For example like Google Maps with a color line?

    Thx
     

    Attached Files:

    • Path.PNG
      Path.PNG
      File size:
      474.4 KB
      Views:
      228
  5. TAK

    TAK Member Licensed User

    where is the mistake in the Code? I think Latitude and Latitude are wrong.

    thx
    Code:
    Sub Process_Globals
    End Sub

    Sub Globals
        
    Dim MapView1 As MapView
        
    Dim PathOverlay1 As PathOverlay
        
    Dim TouchEventsOverlay1 As TouchEventsOverlay
        
    Dim GeoPoint1 As GeoPoint
        
    Dim GPS1 As GPS
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        MapView1.Initialize(
    "")
        MapView1.SetMultiTouchEnabled(
    True)
        MapView1.SetZoomEnabled(
    True)
        
    Activity.AddView(MapView1, 00100%x100%y)
       
        MapView1.Zoom=
    14
        MapView1.SetCenter(
    47.469168.48367)
       
        PathOverlay1.Initialize(MapView1, 
    Colors.Red)
        PathOverlay1.StrokeWidth=
    4dip
        MapView1.AddOverlay(PathOverlay1)
       
        TouchEventsOverlay1.Initialize(
    "TouchEventsOverlay1")
        MapView1.AddOverlay(TouchEventsOverlay1)
       
       
        GeoPoint1.Initialize(
    00)
            
    If FirstTime Then
           
            GPS1.Initialize(
    "GPS")
           
            
    End If
    End Sub

    Sub Activity_Resume
    GPS1.Start(
    1,1'Listen to GPS with no filters.
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
    End Sub

    Sub GPS_LocationChanged (Location1 As Location)
        GeoPoint1.Latitude = Location1.Latitude
        
    Log(Location1.Latitude)
        GeoPoint1.Longitude = Location1.Longitude
        
    Log(Location1.Longitude)
        PathOverlay1.AddPoint3(GeoPoint1)
        MapView1.Invalidate
       
       
    End Sub
     
  6. warwound

    warwound Expert Licensed User

    What do you see on your map and in the log when you run your program?

    Can you upload a working b4a project that i can run and debug myself?
     
  7. TAK

    TAK Member Licensed User

    thx
    the Path (red line) should follow my movement and draw a line. But it seems that the path is in Africa but i am from Europe...
     

    Attached Files:

  8. warwound

    warwound Expert Licensed User

    In your sub GPS_LocationChanged you ought to Dim a new GeoPoint object each time the sub is called.
    Re-using GeoPoint1 will have unpredictable results.

    Martin.
     
    TAK likes this.
  9. Spectre

    Spectre Active Member Licensed User


    Hi Warwound ... After so long ... Any news on the development of the 4.1 that I had tried in beta?
     
  10. warwound

    warwound Expert Licensed User

    I haven't developed the OSMDroid library any further than what you can see available here:
    http://b4a.martinpearman.co.uk/osmdroid/
     
    thedesolatesoul likes this.
  11. Spectre

    Spectre Active Member Licensed User

    Ok ... for distribution licenses are valid from the old version?
     
  12. warwound

    warwound Expert Licensed User

    thedesolatesoul likes this.
  13. rayofcumbria

    rayofcumbria Member Licensed User

    Hi,
    I'm using osmdroid 4.1 (fantastic library :)). Has anyone successfully set the orientation so the direction pointer is pointing upwards?

    big thanks in advance.
     
  14. Spectre

    Spectre Active Member Licensed User

    Hi!
    You must use the set property map orientation...

    MapView1.SetMapOrientation(degree)
     
  15. nemiroG1

    nemiroG1 Member Licensed User

    Martin,
    First off, thank you so much for all your hard work on this project! It is absolutely amazing!

    I have two questions (as I am quite new at this).

    1. Is it possible to use and/or convert Chartbundle files with OSMDroid? I am needing aviation type charts, and trying to figure out how to get them. Right now, chartbundle looks like the only way, but again, I am new at this.


    2. I read your much earlier post about the black bands that sometimes appears when rotating or changing the map orientation. Has there been any progress on this front?

    Thanks, again!
     
  16. warwound

    warwound Expert Licensed User

    Look here: http://www.chartbundle.com/charts/
    Scroll to the section named 'Mobile Atlas Creator(MOBAC)'.

    You can use MOBAC to download the ChartBundle tiles on a computer and create an offline tile archive.
    You can then use this offline tile archive with OSMDroid.
    Look at this forum search for more info on using MOBAC:
    http://www.b4x.com/android/forum/pages/results/?query=mobac&page=1&prefix=0

    An offline tile archive has a few disadvantages:
    • Covering anything but a small area will result in a large (or very large) tile archive.
      Including a large tile archive with your compiled .apk is generally impractical.
      So you then need to host the tile archive online and download it to your b4a application once the application is installed.
    • If the Chartbundle data changes then your offline tiles will be out of date and display old and/or invalid data.
      You need to monitor the Chartbundle data and be sure to update your offline tile archive whenever the Chartbundle data is changed.

    The Chartbundle page (linked to above) offers for download various .xml files that can be used to configure MOBAC.
    Look at this one, 'Sectional Charts':

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <customMapSource>
    <name>Chartbundle US Sectional Charts</name>
    <minZoom>
    4</minZoom>
    <maxZoom>
    13</maxZoom>
    <tileType>png</tileType>
    <tileUpdate>None</tileUpdate>
    <url>http://wms.chartbundle.com/tms/v1
    .0/sec/{$z}/{$x}/{$y}.png?type=google</url>
    <backgroundColor>#000000</backgroundColor>
    </customMapSource>
    This xml tells MOBAC all it needs to know in order for it to connect to and download tiles from this tile server.
    Version 4.1 of my b4a OSMDroid library has an object named OSMDroid_UrlTileSource that's used to display tiles from an online tile server.
    The OSMDroid_UrlTileSource object's Initialize method is documented:

    So i'm thinking that you might be able to use OSMDroid version 4.1 and it's UrlTileSource.
    Get the urls of the tile servers from the various MOBAC xml configuration files and try those urls with the UrlTileSource.
    With a bit of luck it'll work perfectly and display tiles on the map with no need for an offline tile archive.

    Version 4.1 of OSMDroid was never properly uploaded to the forum, it's available for download from my server here:
    http://b4a.martinpearman.co.uk/osmdroid/
    That link should show some version 4.1 examples available to download and one example is OSMDroid_4.1_UrlTileSource.zip which you should find useful.

    Version 4.1 also has better support for map rotation - i'm not sure if the problem with blank areas appearing was fixed or not.
     
    nemiroG1 and thedesolatesoul like this.
  17. nemiroG1

    nemiroG1 Member Licensed User

    Martin,
    Thank you so much for this detailed response! I will work with this here in just a little bit. I travel to the UK from time to time. Next time I do, I need to look you up, as I owe you a pint or two. In the meantime, paypal donation sent!
     
    warwound likes this.
  18. PABLO2013

    PABLO2013 Active Member Licensed User

    OSM Binary Format
    GREETINGS MARTIN
    HOW I CAN USE OSM LIBRARY TO READ A MAP OSM BINARY FORMAT ... OR you indicate ME AS I DO
    TKS
     
  19. warwound

    warwound Expert Licensed User

    You can use the MapsForgeTileSource to display binary OSM data on an OSMDroid map as long as the OSM data is in the MapsForge format.
    There's a version of the MapsForgeTileSource for OSMDroid 3.0.8 as well as a version for the newer OSMDroid 4.1.

    Search the forum for MapsForgeTileSource and you'll find more info.
     
  20. nemiroG1

    nemiroG1 Member Licensed User


    I am needing to use this offline for now, and I have gotten it to work, although it is not perfect. I am trying to improve it all of the time. I used MOBAC to download the Chartbundle aviation sectional charts in the OSMDroid .zip format. After it was down, you need to extract the whole archive, and rename the root folder to "MapquestOSM" (exactly as shown in between the quotes). In the B4A IDE, the map tile source will need to be set to MapquestOSM to match. I put the .zip file in the /osmdroid folder on the device, and so far, it is working! Using a slightly modified version of your offline tiles code:

    Code:
    Version=4.2
    IconFile=
    NumberOfModules=
    0
    Build1=Default,uk.co.martinpearman.b4a.mapviewtutorial
    DoNotOverwriteManifest=
    False
    ManifestCode=
    NumberOfFiles=
    1
    File1=chartbundle.zip                   
    '<-----------  Change here
    NumberOfLibraries=2
    Library1=core
    Library2=osmdroid_4_1            
    '<-----------  I used OSMDroid 4.1, and it all worked fine
    @EndOfDesignText@
    #Region Module Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
        
    #ApplicationLabel: MapView Simple Example
        
    #VersionCode: 1
        
    #VersionName
        
    #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    Sub Process_Globals
        
    Dim MapCenter As GeoPoint
        
    Dim ZoomLevel As Int
    End Sub

    Sub Globals
        
    Dim MapView1 As MapView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    If FirstTime Then
            
    Dim OfflineTileCacheFilename, TileCacheDir As String
            OfflineTileCacheFilename=
    "chartbundle.zip"                        '<-----------  Change here
            TileCacheDir=File.DirRootExternal&"/osmdroid"
            
    '    check if the offline tile cache has already been copied to the OSMDroid cache folder
            If File.Exists(TileCacheDir, OfflineTileCacheFilename)=False Then
                
    '    create the 'osmdroid' cache folder if it does not exist
                If File.Exists(TileCacheDir&"/""")=False Then
                    
    File.MakeDir(File.DirRootExternal, "osmdroid")
                
    End If
                
    '    copy the offline tile cache to the OSMDroid cache folder
                File.Copy(File.DirAssets, OfflineTileCacheFilename, TileCacheDir, OfflineTileCacheFilename)
                MapView1.SetUseDataConnection(
    False)            '<-----------  Change here
            End If
        
    End If
       
        MapView1.Initialize(
    "MapView1")
        
    Activity.AddView(MapView1, 00100%x100%y)
       
        MapView1.SetMultiTouchEnabled(
    True)
        MapView1.SetZoomEnabled(
    True)
       
        
    '    ensure that the map displays the Mapnik tiles
        MapView1.SetTileSource("MapquestOSM")                  '<-----------  Change here
        '    disable the data connection
        MapView1.SetDataConnectionEnabled(False)
       
        
    If FirstTime Then
            
    '    center the map within the area covered by the offline tiles
            MapCenter.Initialize(52.751920.40505)
            
    '    choose a zoom level for which offline tiles exist
            ZoomLevel=10
        
    End If
       
        MapView1.GetController.SetZoom(ZoomLevel)        
    '<-----------  Change here
        MapView1.GetController.SetCenter(MapCenter)       '<-----------  Change here
    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        MapCenter=MapView1.GetCenter                   
    '<-----------  Change here
        ZoomLevel=MapView1.GetZoomLevel             '<-----------  Change here
        MapView1.SetDataConnectionEnabled(False)    '<-----------  Change here
    End Sub

    Sub MapView1_ZoomChanged
        
    'If MapView1.Zoom>12 Then
        '    '    i could have set the map zoom to 12 if it was more than 12 but this way you can see the level 12 tiles stretched to display where zoom level 13+ does not exist
        '    ToastMessageShow("Tiles are cached for zoom levels 0 to 12 only, current zoom level is: "&MapView1.Zoom, False)
        MapView1.SetDataConnectionEnabled(False)                    '<-----------  Change here
        MapView1.GetController.SetCenter(52.751920.40505)      '<-----------  Change here
        'End If
    End Sub





    Think I edited that right, but I just took your code and modified what I had to change, for the sake of this post. My problems are these:

    1. I cannot seem to get an icon on the map, showing my location. The map comes up, and it moves all the right ways, just no reference point.

    2. I cannot seem to display the scale bar. I have tried several iterations, and no joy. Both this and #1 worked in 3.0.8

    3. Is there a way to move the "pivot point" and hence my actual location to the bottom of the screen, so that more of the map is visible "in front of me"? The reason I ask is that I am trying to do something aviation related here, and since airplanes cannot back up, what is in front of me is much more important than anything behind me. As a result, seeing a third or half a screen more of what is in front of me is pretty useful.

    4. Is there a way to allow me to manually pan the map (Which I can do), but set a delay for how long it will be until the map tries to update and re-center? This would be handy when you are trying to look at something, and get more than a second to look at the map, before it tries to slew back to the GPS coordinate.

    Snips from my code below:

    Code:
    Sub Process Globals
        
    Dim InitialTileSourceName As String = "MapquestOSM"
        
    Dim SelectedTileSource As OSMDroid_TileSource
        
    Dim CurrentGeoPoint As OSMDroid_GeoPoint

        
    Dim MapView1 As OSMDroid_MapView
        
    Dim MinimapOverlay1 As OSMDroid_MinimapOverlay
        
    Dim ScaleBarOverlay1 As OSMDroid_ScaleBarOverlay
        
    Dim TileSourceSpinner As Spinner
        
    Dim MapOrientation As Int
        
    Dim TileSourceFactory1 As OSMDroid_TileSourceFactory

        
    '   create some variables to save the MyLocationOverlay state
        Dim CompassEnabled, FollowLocationEnabled, MyLocationEnabled As Boolean
    End Sub

    Sub Globals   
        
    Dim MapView1 As OSMDroid_MapView
        
    Dim MinimapOverlay1 As OSMDroid_MinimapOverlay
        
    Dim ScaleBarOverlay1 As OSMDroid_ScaleBarOverlay
        
    Dim TileSourceSpinner As Spinner
        
    Dim MapOrientation As Int
        
    Dim TileSourceFactory1 As OSMDroid_TileSourceFactory
          
    '   create the MyLocationOverlay
        Dim MyLocationOverlay1 As OSMDroid_MyLocationOverlay
    End Sub


    Sub Activity_Create(FirstTime As Boolean)
        
    Dim TileSourceFactory1 As OSMDroid_TileSourceFactory
        
    Dim AvailableTileSources() As OSMDroid_TileSource


        TileSourceSpinner.Initialize(
    "TileSourceSelect")    'Spinner on settings panel for selecting where map tiles are coming from


        
    '------- Set up the map view ----------------------   
        '   no EventName is required as we don't need to listen for MapView events
        MapView1.Initialize("")
        MapPanel.AddView(MapView1, 
    00, MapPanel.Width, MapPanel.Height)
        
    '   initialize and add the MyLocationOverlay to the MapView, an EventName is used as we'll be listening for all three events that this overlay generates
        MyLocationOverlay1.Initialize("MyLocationOverlay1", MapView1)
        
    '   enable the built in zoom controller - the map can now be zoomed in and out
        MapView1.SetBuiltInZoomControls(True)     
        
    '   enable the built in multi touch controller - the map can now be 'pinch zoomed'
        MapView1.SetMultiTouchControls(True)
        
    '---------------------------------------------------

       
    If FirstTime Then
            
    '-------- Setup Map View and Cached Map Tile Files --
            Dim OfflineTileCacheFilename, TileCacheDir As String
            OfflineTileCacheFilename=
    "chartbundle.zip"
            TileCacheDir=
    File.DirRootExternal&"/osmdroid"
            
    '    check if the offline tile cache has already been copied to the OSMDroid cache folder
            If File.Exists(TileCacheDir, OfflineTileCacheFilename)=False Then
                
    '    create the 'osmdroid' cache folder if it does not exist
                If File.Exists(TileCacheDir&"/""")=False Then
                    
    File.MakeDir(File.DirRootExternal, "osmdroid")
                
    End If
                
    '    copy the offline tile cache to the OSMDroid cache folder
                File.Copy(File.DirAssets, OfflineTileCacheFilename, TileCacheDir, OfflineTileCacheFilename)
            
    End If

            
    '   by default the map will zoom in on a double tap and also be draggable - no other user interface features are enabled
            '   set the zoom level BEFORE the center (otherwise unpredictable map center may be set)
            MapCenter.Initialize(0.00.0)        'Center to start off the app
            MapView1.GetController.SetZoom(9)        'Set the initial zoom to 9

            SelectedTileSource = TileSourceFactory1.GetTileSource(InitialTileSourceName)

            MapView1.SetMapOrientation(
    0)    'Initially set the map up to not rotate, but face north (0°)
      End If



    '....snip....
    'After setting up GPS and other items, I put this in

        
    '   ensure that the MinimapOverlay is the LAST overlay added to the MapView
        MinimapOverlay1.Initialize(MapView1)                         '<-------------This works fine
        MinimapOverlay1.Enabled = True
        MapView1.GetOverlays.Add(MinimapOverlay1)
        cbMiniMap.Checked = 
    True            'ensure that the checkbox for mini map display is already checked
       
        ScaleBarOverlay1.Initialize            
    '<--------------- This is NOT working (worked with OSMDroid 3.0.8)
        ScaleBarOverlay1.SetNautical
        ScaleBarOverlay1.Enabled = 
    True
        MapView1.GetOverlays.Add(ScaleBarOverlay1)
        cbScaleBar.Checked = 
    True            'ensure that the checkbox for scale bar is already checked

      
    '    CompassEnabled = MyLocationOverlay1.CompassEnabled    '<---------- Commented out, as it causes errors.  Worked in OSMDroid 3.0.8
        cbCompass.Checked = False

        MyLocationOverlay1.EnableFollowLocation             
    '<---------- This works fine
        cbFollowLocation.Checked = True
       
        MyLocationOverlay1.EnableMyLocation                 
    '<----------- Cannot see an icon for my 'person'  Worked in OSMDroid 3.0.8
        cbMyLocation.Checked = True
       
        MapView1.SetUseDataConnection(
    False)               '<---------- This works fine
        cbUseOfflineMaps.Checked = True
    End Sub

    Sub Activity Resume
        
    If GPS1.GPSEnabled = False Then
            
    ToastMessageShow("Please enable the GPS device."True)
            
    StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
        Else
            GPS1.Start(
    00'Listen to GPS with no filters.
        End If
    End Sub


    Sub Activity_Pause (UserClosed As Boolean)
        MapCenter=MapView1.GetMapCenter
        TileSource=MapView1.GetTileProvider
        ZoomLevel=MapView1.GetZoomLevel
       
        
    '   save the MyLocationOverlay state
     '   CompassEnabled=MyLocationOverlay1.CompassEnabled
          FollowLocationEnabled = MyLocationOverlay1.IsFollowLocationEnabled
        MyLocationEnabled = MyLocationOverlay1.IsMyLocationEnabled
    End Sub


    '
    ' **********************************************************************************************************************************************
    ' *****************  APP Settings SECTION ******************************************************************************************************
    ' **********************************************************************************************************************************************
    '
    Sub cbMyLocation_CheckedChange(Checked As Boolean)                 '<---------NOT working in OSMDroid 4.1
        If Checked = True Then
            MyLocationOverlay1.EnableMyLocation
        
    Else
            MyLocationOverlay1.DisableMyLocation
        
    End If   
    End Sub


    Sub cbFollowLocation_CheckedChange(Checked As Boolean)        '<---------- Working fine in OSMDroid 4.1
        If Checked = True Then
            MyLocationOverlay1.EnableFollowLocation
        
    Else
            MyLocationOverlay1.DisableFollowLocation
        
    End If
    End Sub


    Sub cbCompass_CheckedChange(Checked As Boolean)
     
    '   MyLocationOverlay1.CompassEnabled=Not(MyLocationOverlay1.CompassEnabled)   '<---------NOT working in OSMDroid 4.1
    End Sub

    Sub cbScaleBar_CheckedChange(Checked As Boolean)              '<---------NOT working in OSMDroid 4.1
        If Checked = True Then
            ScaleBarOverlay1.Enabled = 
    True
        
    Else
            ScaleBarOverlay1.Enabled = 
    False   
        
    End If
    End Sub

    Sub cbMiniMap_CheckedChange(Checked As Boolean)          '<---------- Working fine in OSMDroid 4.1
        If Checked = True Then
            MinimapOverlay1.Enabled = 
    True
        
    Else
            MinimapOverlay1.Enabled = 
    False   
        
    End If   
    End Sub

    Sub cbRotateMap_CheckedChange(Checked As Boolean)     '<---------- Working fine in OSMDroid 4.1
        If Checked = True Then
            MapView1.SetMapOrientation(
    360 - CurrentGPSBearing)  'Sets map orientation by whatever the GPS bearing is
        Else
            MapView1.SetMapOrientation(
    0)  'Sets map orientation to face north (0°)
        End If   
    End Sub

    Sub cbCenterOnLocation_CheckedChange(Checked As Boolean)     '<---------- Working fine in OSMDroid 4.1
        If Checked = True Then
            MapView1.GetController.SetCenter(CurrentGeoPoint)
        
    Else

        
    End If
    End Sub

    Sub cbUseOfflineMaps_CheckedChange(Checked As Boolean)    '<---------- Working fine in OSMDroid 4.1
        If Checked = True Then
            MapView1.SetUseDataConnection(
    False)
        
    Else
            MapView1.SetUseDataConnection(
    True)
        
    End If   
    End Sub

    Sub TileSourceSpinner_ItemClick (Position As Int, Value As Object)         '<---------- Working fine in OSMDroid 4.1
    '    TileSourceSpinner.SelectedIndex=TileSourceSpinner.IndexOf(TileSource)
        'set the MapView Tile Source
        'TileSourceName = Value
        SelectedTileSource = TileSourceFactory1.GetTileSource(Value)
        MapView1.SetTileSource(SelectedTileSource)
    End Sub
    ' **********************************************************************************************************************************************

    Any help you can lend will be most appreciated. Thanks!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice