Android Question Google maps library: Getting a bounding box for a list of coordinates

Rory Mapstone

Member
Licensed User
Longtime User
I see it is possible to use OSMDoid to create a map view and calculate a bounding box for a list of coordinates. Is there functionality in the google maps library in B4A and b4i to calculate a bounding box and move the camera position include all the points in the map fragment view.

Below returns the center of the box using OSMDroid:
B4X:
Sub createBoundingBox As GeoPoint
    Dim m As Map
    Dim data As JSONParser
    data.Initialize(coords)
    Dim datacoords As Map = data.NextObject
    m = datacoords.get("geometry")
   
    Dim l As List
    l = m.Get("coordinates")
    Dim points As List
    points = l.Get(0)
'    Dim points As Map = data1.Get("coordinates")
    Dim lats As List
    lats.Initialize
    Dim lng As List
    lng.Initialize
    For i= 0 To points.Size-1
        Dim tmparr As List = points.Get(i)
        lats.Add(tmparr.Get(0))
        lng.Add(tmparr.Get(1))
    Next
'    Log(lats)
'    Log(lng)
    'get min and max lat
    Dim minlat As Double = 999999
    Dim maxlat As Double = -999999
    For i =0 To lats.Size -1
        If lats.Get(i) < minlat Then
            minlat = lats.Get(i)
        End If
        If lats.Get(i) > maxlat Then
            maxlat = lats.Get(i)
        End If
    Next
   
    Dim minlng As Double = 999999
    Dim maxlng As Double = -999999
    For i =0 To lng.Size -1
        If lng.Get(i) < minlng Then
            minlng = lng.Get(i)
        End If
        If lng.Get(i) > maxlng Then
            maxlng = lng.Get(i)
        End If
    Next
    Log(minlng)
    Log(maxlng)
    box.Initialize(minlat,minlng,maxlat,maxlng)
    Return box.Center
End Sub

Maybe the solution is to use the point returned by osmdroid in google maps? But I would prefer to stick to one library and fit the map to the bounding box.

Regards
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
I don't use OSMDroid as of yet - but intend to soon. What you need to do, is to create a camera position and then move the camera to that position. Here is some code that will do just that in Google Maps.

B4X:
Sub gmap01_Ready
    gmap01.MapType = gmap01.MAP_TYPE_SATELLITE  ' Map is Satellite View
    Dim cp As CameraPosition
    cp.Initialize(-28.255463, 29.116803, 18) ' Use your co-ordinates here
    gmap01.MoveCamera(cp) ' Now move the camera to those co-ordinate - also look at MoveCamera2
    
    Private LatLong As LatLng
    LatLong.Initialize(-28.255463, 29.116803) ' Create a latlong group
    
    Dim m As Marker ' Create your Marker
    m = gmap01.AddMarker2(-28.255463, 29.116803, "myLocation", File.GetUri(File.DirAssets, "myMarkerPin.png"))
       
    Private r = 50 As Double - Create a Red circle
    Circle1 = gmap01.AddCircle(LatLong, r, 2, fx.Colors.Red, fx.Colors.Red, 0.25) Place the circle around your Marker
         
End Sub

So to round off - use the co-ordinate that you get to determine the centre of the box in your code above and then plug them in the co-ordinates in the code above and you will move the map, place a marker and a red circle at that spot.

Enjoy
 
Upvote 0

Rory Mapstone

Member
Licensed User
Longtime User
I don't use OSMDroid as of yet - but intend to soon. What you need to do, is to create a camera position and then move the camera to that position. Here is some code that will do just that in Google Maps.

B4X:
Sub gmap01_Ready
    gmap01.MapType = gmap01.MAP_TYPE_SATELLITE  ' Map is Satellite View
    Dim cp As CameraPosition
    cp.Initialize(-28.255463, 29.116803, 18) ' Use your co-ordinates here
    gmap01.MoveCamera(cp) ' Now move the camera to those co-ordinate - also look at MoveCamera2
  
    Private LatLong As LatLng
    LatLong.Initialize(-28.255463, 29.116803) ' Create a latlong group
  
    Dim m As Marker ' Create your Marker
    m = gmap01.AddMarker2(-28.255463, 29.116803, "myLocation", File.GetUri(File.DirAssets, "myMarkerPin.png"))
     
    Private r = 50 As Double - Create a Red circle
    Circle1 = gmap01.AddCircle(LatLong, r, 2, fx.Colors.Red, fx.Colors.Red, 0.25) Place the circle around your Marker
       
End Sub

So to round off - use the co-ordinate that you get to determine the centre of the box in your code above and then plug them in the co-ordinates in the code above and you will move the map, place a marker and a red circle at that spot.

Enjoy
Thanks for the reply, I actually tried this but then the zoom level becomes a problem. To simulated a bounding box one would have to get the correct zoom level.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See Post #6
telegram047.png

You can directly post a link to that post.
Rightclick on the number and copy the link behind....

See this #6
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
telegram047.png

You can directly post a link to that post.
Rightclick on the number and copy the link behind....

See this #6
Thanks Big Brother - always wondered how you did it.
 
Upvote 0

Similar Threads

Top