iOS Question GoogleMap: uncorrect behaviour of gextra.ZoomToPoints

marcick

Well-Known Member
Licensed User
Longtime User
In this attache code , I simply put 2 markers on the map and then use the function ZoomToPoints to adjust the map center/zoom and show them.
With some couple of coordinates like these, it happens one marker is outside the map, as in this picture (the upper marker is in the area of the top bar).

87fb671e-84a1-46d7-84f7-e87110c0a33e.jpg


Infact, the result of ShowBounds confirm that the map can't show the upper marker because it is outside the bounds.
So looks like the ZoomToPoints function fails sometime.
In other cases the two markers are inside the map, it depends on the couple of coordinates and the result is different.

I Tried to increase the padding in the ZoomToPoints function and can be considered a workaround but not nice because the two markers are not well centered in the available area.

So I'm thinking to another way to obtain the "ZoomToPoints". Any help is appreciated.

edit: I can't attach the project as ZIP because seems too large (??).
This is the code

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private gmap As GoogleMap
    Private ApiKey As String = "AIzaSyAvyeV3neg9Jf-R2Y0Hh_zD-g8Jv-6Hx5Q"
    Dim gextra As GoogleMapsExtra
    Private Panel1 As Panel
    Private PointList As List
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("1")
    NavControl.ShowPage(Page1)
    NavControl.ToolBarVisible=False
    NavControl.NavigationBarVisible=False
    PointList.Initialize
    Dim ll As LatLng
    ll.Initialize(46.57174,8.93102)
    PointList.Add(ll)
    Dim ll As LatLng
    ll.Initialize(45.57148, 8.93083)
    PointList.Add(ll)
    gmap.Initialize("gmap", ApiKey)
    Panel1.AddView(gmap, 0, 0, 100%X, 100%y)
    Dim ll As LatLng=PointList.Get(0)
    gmap.AddMarker(ll.Latitude,ll.Longitude,"test1")
    Dim ll As LatLng=PointList.Get(1)
    gmap.AddMarker(ll.Latitude, ll.Longitude, "test2")
    gextra.Initialize(gmap)
    gextra.ZoomToPoints(PointList)  
    ShowBounds
End Sub

Sub gmap_LongClick (Point As LatLng)
    gextra.ZoomToPoints(PointList)
    ShowBounds
End Sub

Sub ShowBounds
    Dim bounds As Object = gextra.GetVisibleBounds
    Dim ne_sw() As LatLng = gextra.GetBoundsNE_SW(bounds)
    Log(ne_sw(0))
    Log(ne_sw(1))
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    If gmap.IsInitialized Then gmap.SetLayoutAnimated(0, 1, 0, 0, Width, Height)
    gextra.ZoomToPoints(PointList)
    ShowBounds
End Sub

and the GoogleMapsExtra class is taken from the tutorial
 
Last edited:

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Here you can focus from the first market to the last one.

B4X:
Private Sub centerCam_Gmap(OriLat As String, OriLng As String, DestLat As String, DestLng As String)
    Dim LatLog1, LatLog2, LatLonResult As LatLng
    LatLog1.Initialize(DestLat,DestLng)
    LatLog2.Initialize(OriLat,OriLng)
    LatLonResult.Initialize((LatLog1.Latitude + LatLog2.Latitude) / 2,(LatLog1.Longitude + LatLog2.Longitude) / 2)
    Dim cp As CameraPosition
    cp.Initialize(LatLonResult.Latitude,LatLonResult.Longitude, 12)
    gmap.AnimateCamera(cp)
End Sub
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Here you can focus from the first market to the last one.

B4X:
Private Sub centerCam_Gmap(OriLat As String, OriLng As String, DestLat As String, DestLng As String)
    Dim LatLog1, LatLog2, LatLonResult As LatLng
    LatLog1.Initialize(DestLat,DestLng)
    LatLog2.Initialize(OriLat,OriLng)
    LatLonResult.Initialize((LatLog1.Latitude + LatLog2.Latitude) / 2,(LatLog1.Longitude + LatLog2.Longitude) / 2)
    Dim cp As CameraPosition
    cp.Initialize(LatLonResult.Latitude,LatLonResult.Longitude, 12)
    gmap.AnimateCamera(cp)
End Sub
How do you decide the proper zoom level when you set cameraposition ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I don't remember this problem in the past, I suppose something has changed in newer release of GoogleMaps.
By the way, I've decided to solve increasing the padding from 10 to 50 in ZoomToPoints of GoogleMapExtras.
Not very nice because the marker centering is not optimal but let's go on.

B4X:
Public Sub ZoomToPoints(Points As List)
    Dim bounds As Object = no.RunMethod("createBoundsIncluding:", Array(Points))
    Dim update As NativeObject
>>>    update = update.Initialize("GMSCameraUpdate").RunMethod("fitBounds:withPadding:", Array(bounds, 50))
    Dim gmapNo As NativeObject = gm
    gmapNo.RunMethod("animateWithCameraUpdate:", Array(update))
End Sub
 
Upvote 0
Top