Android Question Googlemaps CameraChanged

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I am trying to detect when a Google map is changed. IE Dragged, rotated or zoomed. It is fairly clear that the Google Maps "CameraChanged" feature is the key but I am unable to find out how to do this. Below is my feeble attempt at guess working the solution.
Can someone please point me to an example, I am sure there are some somewhere on the forum.

B4X:
Sub mFragmentListener_CameraChanged(Position As CameraPosition)
    Log("CameraChanged")
    Projection1=GoogleMapsExtras1.GetProjection(gmap)
    MarkerLocation = BTSMarker.Position
    ScreenPosition = Projection1.toScreenLocation(MarkerLocation)
    XX = ScreenPosition.X
    YY = ScreenPosition.Y
' Other stuff
End Sub

Regards Roger
 

eurojam

Well-Known Member
Licensed User
Longtime User
Hello Roger,
here is a small example. when the user zoom in to a higher level then maxzoom the map zooms back to maxzoom...

B4X:
Sub Globals
   Dim mFragment As MapFragment
   Dim gmap As GoogleMap
   Dim GoogleMapsExtras1 As GoogleMapsExtras
   Dim maxZoom As Double = 10
   Dim MapPanel As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
   MapPanel.Initialize("")
   Activity.AddView(MapPanel, 0, 0, 100%x, 80%y)
   If mFragment.IsGooglePlayServicesAvailable = False Then
      ToastMessageShow("Google Play services not available.", True)
   Else
      mFragment.Initialize("Map", MapPanel) 'the event for the camera is Map_CameraChange (Position As CameraPosition)
   End If
End Sub
Sub Map_Ready

   Log("map ready")
   gmap = mFragment.GetMap
   If gmap.IsInitialized = False Then
        ToastMessageShow("Error initializing map.", True)
   Else
     gmap.MapType=gmap.MAP_TYPE_TERRAIN
     gmap.MyLocationEnabled = True
   
     'goto SW germany
     Dim cp As CameraPosition
     cp.Initialize(48.706214727530195,9.060724675655365, 7)
     gmap.AnimateCamera(cp)   
   
   End If
End Sub

Sub Map_CameraChange (Position As CameraPosition)
   Log("Zoom " & gmap.CameraPosition.Zoom)
   'If zoomlevel Is greater Then Max zoom Then zoomout
   Dim CameraUpdateFactory1 As CameraUpdateFactory
   Dim CameraUpdate1 As CameraUpdate
  If gmap.CameraPosition.Zoom > maxZoom Then
     CameraUpdate1=CameraUpdateFactory1.ZoomTo(maxZoom)
     GoogleMapsExtras1.AnimateCamera(gmap, CameraUpdate1)
  End If

End Sub
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
eurojam,

Magic stuff, thanks for the code.
Just to pick your brains once more, the action occurs after the map is moved etc. Do you know if its possible to have it update dynamicaly as in "AnyDragListener_Drag(Marker1 As Marker)" type event?
I am moving Imageviews with the map movement and as it is at the moment the images play catch-up. I can live with this but dynamicaly would be nice.

B4X:
Sub Map_CameraChange (Position As CameraPosition)
   'Log("Zoom " & gmap.CameraPosition.Zoom)
    Projection1=GoogleMapsExtras1.GetProjection(gmap)
  
    MarkerLocation = BTSMarker.Position
    ScreenPosition = Projection1.toScreenLocation(MarkerLocation)
    BTSX = ScreenPosition.X
    BTSY = ScreenPosition.Y
    BTSIV.Left = BTSX - BTSIV.Width/2
    BTSIV.Top = BTSY - BTSIV.Height/2
  
    MarkerLocation = LANDMarker.Position
    ScreenPosition = Projection1.toScreenLocation(MarkerLocation)
    LMX = ScreenPosition.X
    LMY = ScreenPosition.Y
    LMIV.Left = LMX - LMIV.Width/2
    LMIV.Top = LMY - LMIV.Height/2
End Sub

Thanks again
Regards Roger
 
Upvote 0
Top