Android Question [Solved]"smoothing" the movement of the Google map marker

jose luis gudino

Active Member
Licensed User
Longtime User
hI
I would like to "smoothing" the movement of the Google map marker,
I got the ValueAnimator function, which allows me to do that but I do not know how I can do it to integrate it into my B4A code

B4X:
  public void run(List<LatLng> polyLineList) {
      if (index < (polyLineList.size() - 1)) {
                index++;
                next = index + 1;
            } else {
                index = -1;
                next = 1;
               // stopRepeatingTask();
                return;
            }
        
        if (index < (polyLineList.size() - 1)) {
           //startPosition = polyLineList.get(index);
           startPosition = carMarker.getPosition();
           endPosition = polyLineList.get(next);
            }
           ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
            valueAnimator.setDuration(3000);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {

                    v = valueAnimator.getAnimatedFraction();
                    lng = v * endPosition.longitude + (1 - v)
                            * startPosition.longitude;
                    lat = v * endPosition.latitude + (1 - v)
                            * startPosition.latitude;
                    LatLng newPos = new LatLng(lat, lng);
                    carMarker.setPosition(newPos);
                    carMarker.setAnchor(0.5f, 0.5f);
                    carMarker.setRotation(getBearing(startPosition, newPos));
                    googleMap.moveCamera(CameraUpdateFactory
                            .newCameraPosition
                                    (new CameraPosition.Builder()
                                            .target(newPos)
                                            .zoom(15.5f)
                                            .build()));


                }
            });         
          valueAnimator.start();
 

jose luis gudino

Active Member
Licensed User
Longtime User
Hallo Jose, can you show us how you did it?
Hi, I'm going to try to give an idea, I programmed this function a long time ago and I don't remember it very well

B4X:
AnimateTo(fm_topic,1000,lat,lon,placa,apuntador_marker,cod_app)

B4X:
Sub AnimateTo(fm As String, Duration As Int, Targetlat As Double, Targetlon As Double, placa As String, i As Int,cod_app As String)

    Dim oldpos = MapOfMarkers.Get(fm) As LatLng
    '**
    '** si no se ha creado el marker por primera vez
    '**
    If oldpos.IsInitialized = False Then
        Dim pos As LatLng
        pos.Initialize(Targetlat, Targetlon)
        MapOfMarkers.Put(fm,pos)
        m(i) = gmap.AddMarker(Targetlat, Targetlon, placa)
        Dim cp As CameraPosition
        cp.Initialize(Targetlat, Targetlon, 16)
        gmap.MoveCamera(cp)
        AttachInfoWindow(m(i),"Placa: "&placa)
        Return
    End If
    If Duration = 0 Then
        Dim punto As LatLng
        punto.Initialize(Targetlat,Targetlon)
        m(i).Position=punto
        Return
    End If
    'ver si no se ha movido y retorna
    'grafica y retorna
    Dim punto_actul As LatLng
    punto_actul.Initialize(Targetlat,Targetlon)
    If (oldpos.Latitude = punto_actul.Latitude) And (oldpos.Longitude = punto_actul.Longitude) Then
        m(i).Position=punto_actul
        MapOfMarkers.Put(fm,punto_actul)
        '=====================
        '// ENVIAR MUCHO TIEMPO DETENIDO SIN
        '//  CAMBIAR POSICION
        Dim  cont_mark_estac = Map_estacionado.Get(fm) As String
        If cont_mark_estac = "null" Then
            Map_estacionado.Put(fm,"1")
        Else
            Dim cnt = cont_mark_estac As Int
            If cnt >= 3 Then
                Map_estacionado.Put(fm,"0")
           
                '**************
                Dim Pushing As pin
                Pushing.apun = i
                Pushing.heading = getBearing(punto_actul,punto_actul)
                Pushing.lat = punto_actul.Latitude
                Pushing.lon = punto_actul.Longitude
                Pushing.fm = fm
                Log($"Grados ${Pushing.heading}"$)
                enviar_a_MQQT(Pushing,cod_app)
                '**************
            Else
                cnt = cnt + 1
                Map_estacionado.Put(fm,cnt)
            End If
        End If
   
        Return
    End If
    '// RESETEA CONTEO DE TIEMPO SIN ENVIAR
    '//
    Map_estacionado.Put(fm,"0")

    Dim StartTime As Long = DateTime.Now
    Dim StartX  As LatLng
    '***********
    ' si ya existe busca el ultimo punto
    '***********
    StartX.Initialize(oldpos.Latitude,oldpos.Longitude)
    Do While DateTime.Now < StartTime + Duration
   
        Dim f As Double = (DateTime.Now - StartTime) / Duration
        Dim lt As Double = StartX.Latitude  + f * (Targetlat - StartX.Latitude)
        Dim ln As Double = StartX.Longitude + f * (Targetlon - StartX.Longitude)
   
        Dim punto As LatLng
        punto.Initialize(lt,ln)
        m(i).Position=punto
   
'        Dim CameraPosition1 As CameraPosition
'        CameraPosition1.Initialize(lt,ln, 15)
        '**************
         Dim Pushing As pin
         Pushing.apun = i
         Pushing.heading = getBearing(StartX,punto)
         Pushing.lat = punto.Latitude
         Pushing.lon = punto.Longitude
         Pushing.fm = fm
         Log($"Grados ${Pushing.heading}"$)
         enviar_a_MQQT(Pushing,cod_app)
        '**************
        StatusBar1.Text = "Connected "&i
        Sleep(10)
    Loop
'
    Dim punto As LatLng
    punto.Initialize(Targetlat,Targetlon)
    m(i).Position =punto
    MapOfMarkers.Put(fm,punto)
End Sub

B4X:
Sub enviar_a_MQQT(Maker_mapa As pin,cod_app As String)
    If mqtt.Connected = False Then Return
    ' TEST
        Dim obj As Object
        obj = serializator.ConvertObjectToBytes(Maker_mapa)
        Dim obj1 As Object
        obj1 = serializator.ConvertBytesToObject(obj)
    ' END TEST
    LogWrite($"PUBLIC: mapas${cod_app}"$)
    mqtt.Publish("mapas"&cod_app, serializator.ConvertObjectToBytes(Maker_mapa))
End Sub

the function "Sub enviar_a_MQQT" is not necessary
 
Last edited:
Upvote 0
Top