Android Question [Solved sort of!] Get bounding box only returns middle point in map????

Mark Read

Well-Known Member
Licensed User
Longtime User
Using OSMDroid (3.0.8), I want to get the coordinates of the four map corners. Unfortunately, the bounding box only gives the center of the map. Can anyone tell me why?

My Project is attached and I am using B4A 3.80.

The log (from lines 65 & 66) shows:

LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **
(BoundingBoxE6) N:47376383; E:15091094; S:47376383; W:15091094
(BoundingBoxE6) N:47376383; E:15091094; S:47376383; W:15091094


** Activity (main) Resume **

Many thanks.

Edit: Forgot to say, if I zoom in once then all is okay. Log shows:

** Activity (main) Create, isFirst = true **
(BoundingBoxE6) N:47376383; E:15091094; S:47376383; W:15091094
(BoundingBoxE6) N:47376383; E:15091094; S:47376383; W:15091094
** Activity (main) Resume **
Click
47.366152,15.090322
(BoundingBoxE6) N:47386495; E:15118560; S:47366269; W:15063629
 

Attachments

  • PlayingWithMapView.zip
    6.9 KB · Views: 156
Last edited:

Mark Read

Well-Known Member
Licensed User
Longtime User
This has been bugging my happiness for days. Now I have a solution but do not why it works.

Before calling the boundingbox function I inserted a timer. During the rapid debugger I noticed that the boundingbox changed after a short time without a click event???? I set the time to 3 seconds and it works. Now I have reduced the timer to 10ms and it still works. Taking away the timer gives the wrong results. I have no idea why it works but I can live with this.

Now I can continue with the second challenge....

Current code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim TileSource As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim MapView1 As MapView
    Dim MapCenter As GeoPoint
    Dim TouchEventsOverlay1 As TouchEventsOverlay
    '   create the SimpleLocationOverlay
       Dim SimpleLocationOverlay1 As SimpleLocationOverlay
    Dim BoundaryBox As BoundingBox
    Dim coords As String
    Dim Nord(4) As String
    Dim NordValues(4) As Double
    Dim Timer1 As Timer        'Used to create a delay
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    '   pass an EventName when initializing the MapView
       '   MapView has two events: CenterChanged and ZoomChanged
   
    Timer1.Initialize( "Timer1",10)
    MapView1.Initialize("MapView1")
    Activity.AddView(MapView1, 0, 0, 100%x, 100%y)
   
    MapView1.SetMultiTouchEnabled(True)
    MapView1.SetZoomEnabled(True)
    MapCenter.Initialize(47.3763900,15.0911300)    'Leoben as center
    TileSource="MapquestOSM"
   
   
    MapView1.Zoom=15
    MapView1.SetCenter(47.3763900,15.0911300)
    MapView1.SetTileSource(TileSource)
    Activity.Title="OSMDroid ["&MapView1.Zoom&"]"
   
    'Log("MapView1.Center: " & MapView1.GetCenter)
               
    'initialize the SimpleLocationOverlay and add it to the MapView
       SimpleLocationOverlay1.Initialize(MapView1)
       MapView1.AddOverlay(SimpleLocationOverlay1)
       
      'set the SimpleLocationOverlay to display it's icon at the map center
      SimpleLocationOverlay1.SetMyLocation3(MapView1.GetCenter)
   
    TouchEventsOverlay1.Initialize("TouchEventsOverlay1")
    MapView1.AddOverlay(TouchEventsOverlay1)
   
           
    Log("Before Timer1: " & MapView1.GetBoundingBox2(MapView1.Width, MapView1.Height))
    'Log(MapView1.GetBoundingBox)
   
    Timer1.Enabled=True    'Wait for a short period
   
End Sub

Sub GetCoordinates
    Log("After Timer1: " & MapView1.GetBoundingBox2(MapView1.Width, MapView1.Height))
    coords=MapView1.GetBoundingBox2(MapView1.Width, MapView1.Height)
    coords=coords.Replace("(BoundingBoxE6) N:","")
    coords=coords.Replace("E:","")
    coords=coords.Replace("S:","")
    coords=coords.Replace("W:","")
    coords=coords.Replace("; ",",")
   
    Nord=Regex.Split(",",coords)
       
    For i=0 To Nord.Length-1
        NordValues(i)=Nord(i)
        NordValues(i)=NordValues(i)/1000000
        'Log(NordValues(i))
    Next

End Sub

Sub Timer1_Tick
    Log("Timer finished")
    Timer1.Enabled=False
    GetCoordinates
End Sub

'   listen for the MapView ZoomChanged event
Sub MapView1_ZoomChanged
   Log("MapView1_ZoomChanged ZoomLevel="&MapView1.Zoom)
   Activity.Title="OSMDroid ["&MapView1.Zoom&"]"
   Log("ZoomCHangedEvent: " & MapView1.GetBoundingBox2(MapView1.Width, MapView1.Height))
End Sub

Sub TouchEventsOverlay1_Click(GeoPoint1 As GeoPoint)
   Log("Click")
   Log(GeoPoint1.Latitude & "," & GeoPoint1.Longitude)
   Log(MapView1.GetBoundingBox2(MapView1.Width, MapView1.Height))
  
End Sub

Sub TouchEventsOverlay1_LongClick(GeoPoint1 As GeoPoint)
   Log("LongClick")
  
End Sub
 
Upvote 0
Top