Android Question GoogleMaps

GiovanniPolese

Active Member
Licensed User
Longtime User
Hi to everybody
I would like to display the map having the gps movement direction always pointing to the top of the screen, like navigation apps do. I guess that it is necessary to rotate the map (or the camera,for me not very clear at the moment).
Thanks in advance
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Use B4XPages.
2. Call this sub once when the program starts:
B4X:
Private Sub SetMapBearing
    Do While True
        If gmap.IsInitialized And gmap.MyLocationEnabled And gmap.MyLocation.IsInitialized Then
            Dim loc As Location = gmap.As(JavaObject).RunMethod("getMyLocation", Null)
            If loc.IsInitialized And loc.BearingValid And loc.Speed > 0.1 Then
                Dim cp As CameraPosition = gmap.CameraPosition
                cp.Initialize2(cp.Target.Latitude, cp.Target.Longitude, cp.Zoom, loc.Bearing, cp.Tilt)
                gmap.AnimateCamera(cp)
            End If
        End If
        Sleep(100)
    Loop
End Sub
3. Add a reference to the GPS library (for the Location object).
 
Upvote 0

GiovanniPolese

Active Member
Licensed User
Longtime User
Great. I alreay saw that it is working. By the way it works also in an "old style" program, that is a program not using B4XPages (Main+Starter service). Don't know whether it has some influence.
Thanks a lot for now.
 
Upvote 0

GiovanniPolese

Active Member
Licensed User
Longtime User
Hi
not having yet enough skills to manage B4XPages (because I use B4A with years intervals and changes in it gave me learning problems), I adapted an old fashion program, as I said. Then I was surprised that I obtained the same result, that is to get the googlemaps map oriented according to the "bearing", with the attached prgram, base on an old Gps program. I am absolutely not a guru, so take my code just as an obsolete hint. As you can see, besides probable errors and misunderstandings, it works.
I post the project for any use or eventual further discussion and improvements.
The mechanism is the following:
In the Starter service, the GPS is initialized. Then, when location changes, the LocationChanged function is called, which calls the following :
B4X:
Sub GotoPosition(loc As Location)
    If gmap.IsInitialized Then
        Dim CameraPosition1 As CameraPosition = gmap.CameraPosition
        CameraPosition1.Initialize2(loc.Latitude, loc.Longitude, CameraPosition1.Zoom, loc.Bearing, CameraPosition1.Tilt)
        gmap.moveCamera(CameraPosition1)
    End If
End Sub
By the way I also Start/Stop drawing the trajectory.
 

Attachments

  • GRoute_.zip
    9.8 KB · Views: 39
Upvote 0

GiovanniPolese

Active Member
Licensed User
Longtime User
Also, I give a small contribute to this very useful community, with the following code calculating the distance between two points. Perhaps such function exists in some library. I didn't find it, so I did it. It seems to work, in my tests.
B4X:
private Sub GeoDist(A As Location,B As Location) As Double
    Dim EarthRadius As Double, PiGrec As Double, X As Double, Y As Double, dLon As Double
    Dim Lat1,Lat2 As Double
    Dim v1,v2 As Double
    
    EarthRadius= 6372.8  ' Km
    PiGrec = 3.1415926535
    
    Lat1 = A.Latitude * PiGrec / 180
    Lat2 = B.Latitude * PiGrec / 180
    
    dLon = (B.Longitude - A.Longitude) * PiGrec / 180
    
    X = Sin(Lat1) * Sin(Lat2) + Cos(Lat1) * Cos(Lat2) * Cos(dLon)
    v1=Cos(Lat2) * Sin(dLon)
    v2=Cos(Lat1) * Sin(Lat2) - Sin(Lat1) * Cos(Lat2) * Cos(dLon)
    Y = Sqrt( v1 * v1 +  v2 * v2)
    
    Return ATan2(Y, X) * EarthRadius*1000 ' meters
End Sub
 
Upvote 0

Johan Hormaza

Well-Known Member
Licensed User
Also, I give a small contribute to this very useful community, with the following code calculating the distance between two points. Perhaps such function exists in some library. I didn't find it, so I did it. It seems to work, in my tests.
B4X:
private Sub GeoDist(A As Location,B As Location) As Double
    Dim EarthRadius As Double, PiGrec As Double, X As Double, Y As Double, dLon As Double
    Dim Lat1,Lat2 As Double
    Dim v1,v2 As Double
   
    EarthRadius= 6372.8  ' Km
    PiGrec = 3.1415926535
   
    Lat1 = A.Latitude * PiGrec / 180
    Lat2 = B.Latitude * PiGrec / 180
   
    dLon = (B.Longitude - A.Longitude) * PiGrec / 180
   
    X = Sin(Lat1) * Sin(Lat2) + Cos(Lat1) * Cos(Lat2) * Cos(dLon)
    v1=Cos(Lat2) * Sin(dLon)
    v2=Cos(Lat1) * Sin(Lat2) - Sin(Lat1) * Cos(Lat2) * Cos(dLon)
    Y = Sqrt( v1 * v1 +  v2 * v2)
   
    Return ATan2(Y, X) * EarthRadius*1000 ' meters
End Sub
B4X:
                Dim l1, l2 As Location
                l1.Initialize2(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude)
                l2.Initialize2(RootMap.Get("LatitudB"), RootMap.Get("LongitudB"))
                'ahora necesitamos la distancia entre nuestra ubicación y la ubicación de destino
                Dim DistanceTo As Long
                DistanceTo = l1.DistanceTo(l2)  'the result is in meter
                Log("Redondeo: " & Round(DistanceTo/1000) & "KM")
 
Upvote 0
Top