Android Question GMaps zoom

clooney48

Active Member
Licensed User
Longtime User
Is it possible to define the zoom level when I show markers in GMaps? For example in this part of the code or is "gmap.CameraPosition.Zoom" sth. different?
B4X:
If i > 0 Then
              Dim cp As CameraPosition
              cp.Initialize(Lat1, Lon1, gmap.CameraPosition.Zoom)
              gmap.AnimateCamera(cp)
End If
 

Noize

Member
Licensed User
Longtime User
Hi Martin,

Here's my code & Question:

I can't zoom on my position when map is ready, what do u think?, Also when I launch the app it shows a message: Error in Sub, Google map isn't initialized.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")   
    If FirstTime Then
        GPS1.Initialize("GPS")                                            ' Initialize GPS
        lblLon.Initialize("")
        lblLat.Initialize("")
        lblSpeed.Initialize("")
        lblSatellites.Initialize("")
        LastMapCenter.Initialize(40.6443475,2.6305006)
        LastMapZoom=0
    End If
    OnMyLocationChangeListener1.Initialize("OnMyLocationChangeListener1")    
If mFragment.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Google Play services not available.", True)
    Else
        mFragment.Initialize("Map", pnlPage2)
End If
End Sub

Sub Map_Ready
    Log("mapa habilitado")
    goglgmap = mFragment.GetMap
    Dim LatLngBoundsBuilder1 As LatLngBoundsBuilder
    LatLngBoundsBuilder1.Initialize
    If goglgmap.IsInitialized = False Then
           ToastMessageShow("Error iniciando el mapa.", True)
    Else
        goglgmap.MapType=goglgmap.MAP_TYPE_HYBRID
        Dim GoogleMapsExtras1 As GoogleMapsExtras
'        Dim TileOverlayOptions1 As TileOverlayOptions
        Dim CameraPosition1 As CameraPosition
        CameraPosition1.Initialize(LastMapCenter.Latitude, LastMapCenter.Longitude, LastMapZoom)
        goglgmap.MyLocationEnabled=True
        goglgmap.GetUiSettings.CompassEnabled=True
        goglgmap.AnimateCamera(CameraPosition1)
        goglgmap.MoveCamera(CameraPosition1)
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    GPS1.Stop
    intcurrentPage = stdbar.SelectedIndex
    LastMapCenter=mFragment.GetMap.CameraPosition.Target
    LastMapZoom=mFragment.GetMap.CameraPosition.Zoom
End Sub
 
Upvote 0

Noize

Member
Licensed User
Longtime User
Martin,

Every time I launch the app it shows that OnMyLocationListener should be Initialized, but I have this sentence on Activity_Create

OnMyLocationChangeListener1.Initialize("OnMyLocationChangeListener1")

any idea?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Martin,

Every time I launch the app it shows that OnMyLocationListener should be Initialized, but I have this sentence on Activity_Create

OnMyLocationChangeListener1.Initialize("OnMyLocationChangeListener1")

any idea?

My library code will look for the existence of the sub named OnMyLocationChangeListener1_MyLocationChange(Location1 As Location), it will fail to initialize the OnMyLocationChangeListener object if it fails to find the callback sub.

Do you have the required sub in your code?
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
A more elegant solution would be something like this:

B4X:
Dim LatLngBoundsBuilder1 As LatLngBoundsBuilder
LatLngBoundsBuilder1.Initialize

Marker1=GoogleMap1.AddMarker(aLat, aLon, "Foobar")
LatLngBoundsBuilder1.Include(Marker1.Position)
    
Marker2=GoogleMap1.AddMarker(aLat2, aLon2, "Foobar2")
LatLngBoundsBuilder1.Include(Marker2.Position)

'   create more Markers and Include each Marker Position in the LatLngBoundsBuilder

Dim MarkerBounds As LatLngBounds=LatLngBoundsBuilder1.Build
GoogleMapsExtras1.AnimateToBounds(GoogleMap1, MarkerBounds, 128)

As you create each Marker add it's LatLng Position to a LatLngBoundBuilder.
Once you've created all your Markers, you get a LatLngBounds from the LatLngBoundBuilder which represents the area of the earth that your Markers cover.

Pass the LatLngBounds to the AnimateToBounds method and the map will adjust it's zoom level and center to fully display all of your Markers.
The 128 parameter is a 'padding' parameter - the map will adjust pan and zoom and then pad the area around your Markers by 128 pixels.

Martin.

While running this code, app crash with this: on last line code
java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
ok. Another question, what about if I have only 1 marker and I need a zoom = 14, How can I do it ?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You can't create a LatLngBounds from a single Marker location.
So i guess you'd count the number of Marker locations you have, if you have 1 then set the map center to that location and set the zoom level to 14.
Otherwise create a LatLngBounds and fit the map to that bounds.
 
Upvote 0
Top