Mapview closing

Nyptop

Active Member
Licensed User
Longtime User
With this code, whenever the screen is pressed the map view disappears. I don't want the map view to disappear. I want it to either: 'not react to user interaction what so ever' or 'users can move the view around but when the location is changed then it snaps back to the new location'. I'm sure its a simple fix but it has stumped me for quite a while :L Thanks in advance! (By the way OSMDroid, fantastic mapview library. I'm loving it! learning java so hopefully in a bit I'll be able to contribute some libraries) Here is the code:

B4X:
Sub Process_Globals
    Dim MapCenter As GeoPoint
    Dim TileSource As String
    Dim ZoomLevel As Int
    
    '    create some variables to save the MyLocationOverlay state
    Dim CompassEnabled, FollowLocationEnabled, MyLocationEnabled As Boolean
End Sub

Sub Globals
    Dim MapView1 As MapView
    Dim MinimapOverlay1 As MinimapOverlay
    Dim ScaleBarOverlay1 As ScaleBarOverlay

    
    '    create the MyLocationOverlay
    Dim MyLocationOverlay1 As MyLocationOverlay
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    update the MenuItems
    Activity.AddMenuItem("Toggle Compass", "MenuItemSelect")
    Activity.AddMenuItem("Toggle MyLocation", "MenuItemSelect")
    Activity.AddMenuItem("Toggle FollowLocation", "MenuItemSelect")
    
    '    MapView initialized with no EventName as we'll not be listening for MapView events
    MapView1.Initialize("")
    Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
   
    MapView1.SetMultiTouchEnabled(True)
    MapView1.SetZoomEnabled(True)
    
    '    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(MapView1, "MyLocationOverlay1")
    MapView1.AddOverlay(MyLocationOverlay1)
    
    If FirstTime Then

        TileSource="Mapnik"
        ZoomLevel=14
        
        '    set the default values for the new variables
        CompassEnabled=MyLocationOverlay1.CompassEnabled
        FollowLocationEnabled=MyLocationOverlay1.FollowLocationEnabled
        MyLocationEnabled=MyLocationOverlay1.MyLocationEnabled
    Else
        '    restore the MyLocationOverlay state
        MyLocationOverlay1.CompassEnabled=CompassEnabled
    End If
    
    MapView1.Zoom=ZoomLevel
    MyLocationOverlay1.MyLocationEnabled = True
   MyLocationOverlay1.FollowLocationEnabled = True
    
    ScaleBarOverlay1.Initialize(MapView1)
    MapView1.AddOverlay(ScaleBarOverlay1)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    MapCenter=MapView1.GetCenter
    TileSource=MapView1.GetTileSource
    ZoomLevel=MapView1.Zoom
    
    '    save the MyLocationOverlay state
    CompassEnabled=MyLocationOverlay1.CompassEnabled
    FollowLocationEnabled=MyLocationOverlay1.FollowLocationEnabled
    MyLocationEnabled=MyLocationOverlay1.MyLocationEnabled
    
    '    disable MyLocationOverlay compass and GPS listening
    MyLocationOverlay1.CompassEnabled=False
    MyLocationOverlay1.MyLocationEnabled=False
End Sub
 
Last edited:

warwound

Expert
Licensed User
Longtime User
If i run the code that you posted then the Activity force closes as soon as i start it.
That's a null pointer exception as it can't find the drawables required by the MyLocationOverlay.

So i copied the library drawable-nodpi folder containing the required drawables to the Objects/res folder and the code compiles and runs as expected.
Dragging tha map causes MyLocationOverlay1.MyLocationEnabled to be auto-disabled, that's the default behaviour of OSMDroid.

Next i added a Panel to the Activity and added a Sub listening for the Panel Touch event.
The Panel captures any Touch events preventing the MapView from receiving those Touch events.
Toggling the Panel size from 0 x 0 to 100%x x 100%y effectively enables or disables any Touch events being received by the MapView:

B4X:
Sub Process_Globals
    Dim MapCenter As GeoPoint
    Dim TileSource As String
    Dim ZoomLevel As Int
    
   Dim MapTouchEnabled As Boolean
   
    '    create some variables to save the MyLocationOverlay state
    Dim CompassEnabled, FollowLocationEnabled, MyLocationEnabled As Boolean
End Sub

Sub Globals
    Dim MapView1 As MapView
    Dim MinimapOverlay1 As MinimapOverlay
    Dim ScaleBarOverlay1 As ScaleBarOverlay

    Dim TouchEventGrabber As Panel
   
    '    create the MyLocationOverlay
    Dim MyLocationOverlay1 As MyLocationOverlay
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    update the MenuItems
    '   Activity.AddMenuItem("Toggle Compass", "MenuItemSelect")
    '   Activity.AddMenuItem("Toggle MyLocation", "MenuItemSelect")
    Activity.AddMenuItem("Toggle FollowLocation", "MenuItemSelect")
    Activity.AddMenuItem("Toggle MapTouchEnabled", "MenuItemSelect")
    
    '    MapView initialized with no EventName as we'll not be listening for MapView events
    MapView1.Initialize("")
    MapView1.SetMultiTouchEnabled(True)
    MapView1.SetZoomEnabled(True)
    Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
    
    '    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(MapView1, "MyLocationOverlay1")
    MyLocationOverlay1.MyLocationEnabled = True
    MyLocationOverlay1.FollowLocationEnabled = True
    MapView1.AddOverlay(MyLocationOverlay1)
    
    If FirstTime Then
      MapTouchEnabled=True
        TileSource="Mapnik"
        ZoomLevel=14
        
        '    set the default values for the new variables
        CompassEnabled=MyLocationOverlay1.CompassEnabled
        FollowLocationEnabled=MyLocationOverlay1.FollowLocationEnabled
        MyLocationEnabled=MyLocationOverlay1.MyLocationEnabled
    Else
        '    restore the MyLocationOverlay state
        MyLocationOverlay1.CompassEnabled=CompassEnabled
    End If
    
    MapView1.Zoom=ZoomLevel
    
    ScaleBarOverlay1.Initialize(MapView1)
    MapView1.AddOverlay(ScaleBarOverlay1)
   
   TouchEventGrabber.Initialize("TouchEventGrabber")
   If MapTouchEnabled Then
      Activity.AddView(TouchEventGrabber, 0, 0, 0, 0)
   Else
      Activity.AddView(TouchEventGrabber, 0, 0, 100%x, 100%y)
   End If
   
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    MapCenter=MapView1.GetCenter
    TileSource=MapView1.GetTileSource
    ZoomLevel=MapView1.Zoom
    
    '    save the MyLocationOverlay state
    CompassEnabled=MyLocationOverlay1.CompassEnabled
    FollowLocationEnabled=MyLocationOverlay1.FollowLocationEnabled
    MyLocationEnabled=MyLocationOverlay1.MyLocationEnabled
    
    '    disable MyLocationOverlay compass and GPS listening
    MyLocationOverlay1.CompassEnabled=False
    MyLocationOverlay1.MyLocationEnabled=False
End Sub

Sub MenuItemSelect_Click
   Dim Id As String
   Id=Sender
   Select Id
      Case "Toggle MapTouchEnabled"
         MapTouchEnabled=Not(MapTouchEnabled)
         If MapTouchEnabled Then
            TouchEventGrabber.Width=0
            TouchEventGrabber.Height=0
         Else
            TouchEventGrabber.Width=100%x
            TouchEventGrabber.Height=100%y
         End If
      Case "Toggle FollowLocation"
         MyLocationOverlay1.FollowLocationEnabled=Not(MyLocationOverlay1.FollowLocationEnabled)
   End Select
End Sub

Sub TouchEventGrabber_Touch (Action As Int, X As Float, Y As Float)
   '   do nothing
End Sub

There is no way to programmatically disable the MapView Touch events.

Martin.
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Hi again,

Everything is working great except for one thing. The map shows an area which is a couple of kilometers out. Sometimes it is right above where the man icon is (where I want the map to be centred). Is my phone the problem or is there something in the code I could amend?

Thanks,

Neil
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi again,

Everything is working great except for one thing. The map shows an area which is a couple of kilometers out. Sometimes it is right above where the man icon is (where I want the map to be centred). Is my phone the problem or is there something in the code I could amend?

Thanks,

Neil

MyLocationOverlay will automatically disable FollowLocation if the user pans or zooms the map.
Sounds to me as if that's what you're seeing.

You can listen for the MyLocationOverlay FollowLocationAutoDisabled event:

B4X:
Sub MyLocationOverlay1_FollowLocationAutoDisabled
    Log("FollowLocation has been disabled")
End Sub

A solution may be to re-enable FollowLocation in that Sub.

Or have a read here: http://www.b4x.com/forum/basic4andr...mdroid-mapview-b4a-tutorial-7.html#post101026

You could use a Timer to re-enable FollowLocation after a set period.

In fact if you listen for the MapView CenterChanged event you could reset the Timer each time the user pans the map.

So if the Timer will re-enable FollowLocation after 10 seconds then that will be 10 seconds after the user last panned the map.

A complete list of all OSMDroid events, methods and properties can be found here: http://www.b4x.com/forum/additional.../16309-osmdroid-mapview-b4a-5.html#post106488

Martin.
 
Upvote 0

Nyptop

Active Member
Licensed User
Longtime User
Thanks Warwound,

Great help. So my code goes something as follows if anyone wants to read this thread for help ('t1' is a timer.):

B4X:
Sub MyLocationOverlay1_FollowLocationAutoDisabled
    t1.Initialize("t1", 5000)
   t1.Enabled = True
End Sub
Sub t1_click
   MyLocationOverlay1.FollowLocationEnabled = True
   t1.Enabled = False
End Sub

Cheers,

Neil
 
Upvote 0

GKTechnology

New Member
Licensed User
Longtime User
With this code, whenever the screen is pressed the map view disappears. I don't want the map view to disappear. I want it to either: 'not react to user interaction what so ever' or 'users can move the view around but when the location is changed then it snaps back to the new location'. I'm sure its a simple fix but it has stumped me for quite a while :L Thanks in advance! (By the way OSMDroid, fantastic mapview library. I'm loving it! learning java so hopefully in a bit I'll be able to contribute some libraries) Here is the code:

B4X:
Sub Process_Globals
    Dim MapCenter As GeoPoint
    Dim TileSource As String
    Dim ZoomLevel As Int
    
    '    create some variables to save the MyLocationOverlay state
    Dim CompassEnabled, FollowLocationEnabled, MyLocationEnabled As Boolean
End Sub

Sub Globals
    Dim MapView1 As MapView
    Dim MinimapOverlay1 As MinimapOverlay
    Dim ScaleBarOverlay1 As ScaleBarOverlay

    
    '    create the MyLocationOverlay
    Dim MyLocationOverlay1 As MyLocationOverlay
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    update the MenuItems
    Activity.AddMenuItem("Toggle Compass", "MenuItemSelect")
    Activity.AddMenuItem("Toggle MyLocation", "MenuItemSelect")
    Activity.AddMenuItem("Toggle FollowLocation", "MenuItemSelect")
    
    '    MapView initialized with no EventName as we'll not be listening for MapView events
    MapView1.Initialize("")
    Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
   
    MapView1.SetMultiTouchEnabled(True)
    MapView1.SetZoomEnabled(True)
    
    '    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(MapView1, "MyLocationOverlay1")
    MapView1.AddOverlay(MyLocationOverlay1)
    
    If FirstTime Then

        TileSource="Mapnik"
        ZoomLevel=14
        
        '    set the default values for the new variables
        CompassEnabled=MyLocationOverlay1.CompassEnabled
        FollowLocationEnabled=MyLocationOverlay1.FollowLocationEnabled
        MyLocationEnabled=MyLocationOverlay1.MyLocationEnabled
    Else
        '    restore the MyLocationOverlay state
        MyLocationOverlay1.CompassEnabled=CompassEnabled
    End If
    
    MapView1.Zoom=ZoomLevel
    MyLocationOverlay1.MyLocationEnabled = True
   MyLocationOverlay1.FollowLocationEnabled = True
    
    ScaleBarOverlay1.Initialize(MapView1)
    MapView1.AddOverlay(ScaleBarOverlay1)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    MapCenter=MapView1.GetCenter
    TileSource=MapView1.GetTileSource
    ZoomLevel=MapView1.Zoom
    
    '    save the MyLocationOverlay state
    CompassEnabled=MyLocationOverlay1.CompassEnabled
    FollowLocationEnabled=MyLocationOverlay1.FollowLocationEnabled
    MyLocationEnabled=MyLocationOverlay1.MyLocationEnabled
    
    '    disable MyLocationOverlay compass and GPS listening
    MyLocationOverlay1.CompassEnabled=False
    MyLocationOverlay1.MyLocationEnabled=False
End Sub

I tried that code but i get the same error that i do its a null pointer exception at mylocationoverlay1.initialize(Mapview1, "MylocationOverlay")
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I tried that code but i get the same error that i do its a null pointer exception at mylocationoverlay1.initialize(Mapview1, "MylocationOverlay")

If i run the code that you posted then the Activity force closes as soon as i start it.
That's a null pointer exception as it can't find the drawables required by the MyLocationOverlay.

So i copied the library drawable-nodpi folder containing the required drawables to the Objects/res folder and the code compiles and runs as expected.

Sounds like it's force closing when it fails to find the drawables that MyLocationOverlay requires.

Martin.
 
Upvote 0
Top