Hello everybody,
I have a little problem with Google Map Library 2.0 and MapFragment Event.
I made an app, using tutorials, which display GPS positions and distance on the screen. At the end of my walking activity, I can press a "Save" button to write in a file the logs of what I did and at the same time, I have a Map Fragment with the path drawn.
Here a screen of my app :
Everything is working well..... Except one thing. When I don't have GPS enabled at the beginning and when I activate it manually, here the logs I get :
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false ** (Because of the gps not enabled)
Ignoring event: mapfragment1_ready
** Activity (main) Resume **
The app is still going but when I want to save and draw the path, I have this exception :
main$ResumableSub_Save_Clickresume (java line: 694)
java.lang.RuntimeException: Object should first be initialized (GoogleMap).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.MapFragmentWrapper$GoogleMapWrapper.AddPolyline(MapFragmentWrapper.java:287)
at test.adrien.main$ResumableSub_Save_Click.resume(main.java:694)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I understood that is because my ready_event has not been considered.... but I don't know how to fix this...
Thank you for your help. I'm a beginner in BXA so feel free to correct things in the following code.
Activity Main :
And my Starter Service code just in case
I have a little problem with Google Map Library 2.0 and MapFragment Event.
I made an app, using tutorials, which display GPS positions and distance on the screen. At the end of my walking activity, I can press a "Save" button to write in a file the logs of what I did and at the same time, I have a Map Fragment with the path drawn.
Here a screen of my app :
Everything is working well..... Except one thing. When I don't have GPS enabled at the beginning and when I activate it manually, here the logs I get :
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false ** (Because of the gps not enabled)
Ignoring event: mapfragment1_ready
** Activity (main) Resume **
The app is still going but when I want to save and draw the path, I have this exception :
main$ResumableSub_Save_Clickresume (java line: 694)
java.lang.RuntimeException: Object should first be initialized (GoogleMap).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.MapFragmentWrapper$GoogleMapWrapper.AddPolyline(MapFragmentWrapper.java:287)
at test.adrien.main$ResumableSub_Save_Click.resume(main.java:694)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I understood that is because my ready_event has not been considered.... but I don't know how to fix this...
Thank you for your help. I'm a beginner in BXA so feel free to correct things in the following code.
Activity Main :
B4X:
Sub Process_Globals
End Sub
Sub Globals
Private shared As String
Dim Awake As PhoneWakeState
Private Lati As Label
Private Longi As Label
Private Satellit As Label
Private Speed As Label
Private Altitude As Label
Private Dist As Label
Private MapFragment1 As MapFragment
Private gmap As GoogleMap
Private Reset As Button
Private Save As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("VuePrincipale")
If MapFragment1.IsGooglePlayServicesAvailable = False Then
ToastMessageShow("Google Play services not available.", True)
End If
End Sub
Sub SaveStringExample
File.WriteList(shared, "PositionsGPS.txt", Starter.List1)
End Sub
Sub Activity_Resume
If Starter.gps.GPSEnabled = False Then
ToastMessageShow("Please enable the GPS device.", True)
StartActivity(Starter.gps.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then CallSubDelayed(Starter, "StartGPS")
End If
End Sub
Public Sub GpsStatus (Satellites As List)
Dim sb As StringBuilder
sb.Initialize
For i = 0 To Satellites.Size - 1
Dim Satellite As GPSSatellite = Satellites.Get(i)
sb.Append(CRLF).Append(Satellite.Prn).Append($" $1.2{Satellite.Snr}"$).Append(" ").Append(Satellite.UsedInFix)
sb.Append(" ").Append($" $1.2{Satellite.Azimuth}"$).Append($" $1.2{Satellite.Elevation}"$)
Next
Satellit.Text = sb.ToString
End Sub
Public Sub GPS_LocationChanged (Location1 As Location,Distance As Double)
Awake.KeepAlive(True)
Altitude.Text = Location1.Altitude
Lati.Text = Round(Location1.Latitude*11930464.7111)
Longi.Text = Round(Location1.Longitude*11930464.7111)
Speed.Text = $"$1.1{Location1.Speed*3.6} km/h "$
Dist.Text = Distance
Awake.ReleaseKeepAlive
End Sub
Sub Activity_Pause (UserClosed As Boolean)
Log("DANS Pause")
If UserClosed Then
Awake.ReleaseKeepAlive
End If
End Sub
Sub MapFragment1_Ready
Log("DANS Map_ready")
gmap = MapFragment1.GetMap
gmap.MyLocationEnabled = True
End Sub
Sub Save_Click
If File.ExternalWritable = False Then
Msgbox("No rights to write"," ")
Return
Else
shared = Starter.rp.GetSafeDirDefaultExternal("test")
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
If Result Then SaveStringExample
End If
Dim pl As Polyline = gmap.AddPolyline
pl.Points = Starter.listLocations
pl.Color=Colors.Blue
pl.ZIndex=3
Save.Enabled = False
End Sub
Sub Reset_LongClick
Starter.Distance = 0
Dist.Text = 0
Starter.cpt = 0
Save.Enabled = True
Starter.listLocations.Clear
Starter.List1.Clear
End Sub
And my Starter Service code just in case
B4X:
Sub Process_Globals
Public rp As RuntimePermissions
Public gps As GPS
Private gpsStarted As Boolean
Public List1 As List
Public StartLocation, EndLocation As Location
Public LatitudeReal, LongitudeReal As Double
Public LatitudeStart, LongitudeEnd As Double
Public listLocations As List
Public Distance As Double = 0
Public cpt As Int = 0
End Sub
Sub Service_Create
gps.Initialize("gps")
listLocations.Initialize
List1.Initialize
End Sub
Sub GPS_LocationChanged (Location1 As Location)
Dim locations As LatLng
Dim sbb As StringBuilder
locations.Initialize(Location1.Latitude,Location1.Longitude)
listLocations.Add(locations)
LatitudeReal = Location1.Latitude
LongitudeReal = Location1.Longitude
If cpt == 0 Then
StartLocation.Initialize2(LatitudeReal, LongitudeReal)
End If
EndLocation.Initialize2(LatitudeReal, LongitudeReal)
Distance = Distance + StartLocation.DistanceTo(EndLocation)
LatitudeStart = LatitudeReal
LongitudeEnd = LongitudeReal
StartLocation.Initialize2(LatitudeStart, LongitudeEnd)
sbb.Initialize
sbb.Append("timestamp," & Chr(34) & (Round(DateTime.Now/1000)-631065600) & Chr(34) & ",s,altitude,"&Chr(34)&(Location1.Altitude)&Chr(34)&",m,distance,"&Chr(34) & Distance & Chr(34) & ",m,speed," & Chr(34) & $"$1.2{Location1.Speed}"$ & Chr(34) & ",m/s,position_lat,"& Chr(34) & (Round(Location1.Latitude*11930464.7111)) & Chr(34) & ",position_long,"&Chr(34) & (Round(Location1.Longitude*11930464.7111))&Chr(34))
List1.Add(sbb.ToString)
cpt = cpt + 1
CallSub3(Main, "GPS_LocationChanged", Location1, Distance)
End Sub
Public Sub StartGps
If gpsStarted = False Then
gps.Start(0, 0)
gpsStarted = True
End If
End Sub
Public Sub StopGps
If gpsStarted Then
gps.Stop
gpsStarted = False
End If
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub GPS_GpsStatus (Satellites As List)
CallSub2(Main, "GpsStatus", Satellites)
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Destroy
StopGps
End Sub