Android Question Google maps location changing even when scrolling is off

kostefar

Active Member
Licensed User
Longtime User
Dear All,


If I put Scroll gestures as enabled in the designer, then yes: I can scroll like I should. Then if I put it to disabled, it almost behaves as it should, but only almost: The location changes when zooming by pinching.

I´m not sure if there´s some sort of security breach if I´d post my google key here, which is why I´m not attaching a zip. Instead, here´s the code from Main, followed by a screenshot of the settings in designer. If you feel like reproducing the problem, you can see the numbers in the activity changes the more you zoom out with pinching. With +/- this is not a problem.

You can also see that the red circle changes position on the screen. In fact the circle stays where it is, covering the exact same area in pixels, but obviously the physical area in km changes when zoom changes. It´s the map that changes the position, making it look like the circle changes.

B4X:
#Region  Project Attributes
    #ApplicationLabel: GoogleMapDemo
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
   #AdditionalJar: com.google.android.gms:play-services-maps
#End Region
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#BridgeLogger: true
Sub Process_Globals
End Sub
Sub Globals
    Dim CameraUpdateFactory1 As CameraUpdateFactory
    Dim CameraUpdate1 As CameraUpdate
    Dim CameraPosition1 As CameraPosition
    Dim staticradius As Float = 8
    Dim zoomlevel As Float = 21
    Dim Panel1 As Panel
    Dim MapFragment1 As MapFragment
    Dim GoogleMap1 As GoogleMap
    Dim GoogleMapsExtras1 As GoogleMapsExtras
    Dim MapPanel As Panel
    Dim CircleOptions1 As CircleOptions
    Dim Circle2 As Circle
    Dim lat As Float = 52.7454476
    Dim longi As Float = 8.736779
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
End Sub
Sub Mapfragment1_Camerachange(Position As CameraPosition)
    Dim newzoom As Float = 16-(Position.zoom-7)
    Dim radius As Float = Power(2,newzoom)
    Activity.Title = Round2((newzoom-1),1)  & " radius " & Round2(radius,1) & " LO " & Round2(Position.Target.Longitude,2) & " LA " & Round2(Position.Target.Latitude,2)
    If Circle2.IsInitialized Then Circle2.Radius = radius
End Sub
Sub MapFragment1_Ready
    GoogleMap1 = MapFragment1.GetMap
        CircleOptions1.Initialize
        CircleOptions1.Center2(lat, longi).FillColor(Colors.ARGB(128, 192, 0, 0)).Radius(staticradius).StrokeColor(Colors.Black).StrokeWidth(2)
        Circle2=GoogleMapsExtras1.AddCircle(GoogleMap1, CircleOptions1)
        CameraPosition1.Initialize(lat, longi, zoomlevel)
        GoogleMap1.AnimateCamera(CameraPosition1)
End Sub

iSu0ZbC.png


Any idea why this is happening?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The location changes when zooming by pinching.
You mean that the camera location changes, right?

Tip:
B4X:
Activity.Title = Round2((newzoom-1),1) & " radius " & Round2(radius,1) & " LO " & Round2(Position.Target.Longitude,2) & " LA " & Round2(Position.Target.Latitude,2)
- Use Log to print such messages.
- Use smart string literal:
B4X:
Log($"$1.1{newzoom-1} radius $1.1{radius} LO $1.2{Position.Target.Longitude} LA $1.2{Position.Target.Latitude)"$)

The central point will change when the user zooms in based on the gesture point.
You can disable this gesture and two buttons to let the user zoom in or out.
Another option is to handle the CameraChange event and recenter the map by calling gmap.MoveCamera with the central point and the current zoom.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thanks Erel,

Usually I use logging and I probably should have done it here to let you see the difference in longitude and latitude but you figured it out :)
Here it is anyway:

B4X:
9.9 radius 1942.8 LO 8.74 LA 52.75
11.2 radius 4627.9 LO 8.79 LA 52.76
12.5 radius 11792.3 LO 8.72 LA 52.8

The smart string literal I did not know about, thanks!


Do you mean disabling the only two options I have enabled, as per the designer screenshot?
Do you then mean to use gestures detector library? That´s something I´ve looked into a bit already, and may proceed if that´s the only way.

With the recentering, do you mean:

B4X:
Sub Mapfragment1_Camerachange(Position As CameraPosition)
    Dim newzoom As Float = 16-(Position.zoom-7)
    Dim radius As Float = Power(2,newzoom)
    Activity.Title = Round2((newzoom-1),1)  & " radius " & Round2(radius,1) & " LO " & Round2(Position.Target.Longitude,2) & " LA " & Round2(Position.Target.Latitude,2)
   
    If Circle2.IsInitialized Then Circle2.Radius = radius
    CameraPosition1.Initialize(lat,longi,Position.zoom)
    GoogleMap1.MoveCamera(CameraPosition1)

End Sub

If I do this, I struggle alot to get it to zoom out - many pinches to get it to change.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I meant that you can disable the zoom gesture and provide a different way to zoom in or out. For example with two buttons.

Code to recenter the map after the user has finished moving it:
B4X:
Sub Mapfragment_CameraChange(Position As CameraPoition)
 ActionIndex = ActionIndex + 1 'global int variable.
 Dim MyIndex As Int = ActionIndex
 Sleep(300)
 If MyIndex = ActionIndex Then
   'move the camera here
 End If
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
I meant that you can disable the zoom gesture and provide a different way to zoom in or out. For example with two buttons.

Code to recenter the map after the user has finished moving it:
B4X:
Sub Mapfragment_CameraChange(Position As CameraPoition)
ActionIndex = ActionIndex + 1 'global int variable.
Dim MyIndex As Int = ActionIndex
Sleep(300)
If MyIndex = ActionIndex Then
   'move the camera here
End If


Thanks Erel, that improved it a bit but still I have to pinch alot to get it to zoom. After the above code, you´d just put:

B4X:
    CameraPosition1.Initialize(lat,longi,Position.zoom)
    GoogleMap1.MoveCamera(CameraPosition1)

Right?

lat and longi are the static values of the position.

Anyway, if it can´t be done in a smooth looking way with pinching, I´ll be using a seekbar instead for the zooming.
 
Last edited:
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thanks I also tried that yesterday, 800 I think, but saw no difference. I´ll try with even higher values later, but if this does not help, I´ll just go with the seekbar solution.
 
Upvote 0
Top