Android Question How to Enable GPS Location Service

AndroidMadhu

Active Member
Licensed User
Hello,

I am trying to enable GPS location service manually asking user to Enable it.
Though I have put
B4X:
android.hardware.location.gps
the permission at manifest file still I am not able to activate the Location service at my mobile.



Please suggest on this....
 

AndroidMadhu

Active Member
Licensed User
@Erel,
I have checked the above solution. Unfortunately I am getting the error as "fusedlocationprovider is missing"...




Could you please suggest on this please
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
I am getting while running fusedlocationprovider [package Does not exists]. Though i have copied the library at library path


the codebase from starter.bas
B4X:
#Region   Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region

Sub Process_Globals
Public rp As RuntimePermissions
Public flp As FusedLocationProvider
' Dim sql As SQL
End Sub

Sub Service_Create
flp.Initialize("flp")
flp.Connect
'If File.Exists(File.DirInternal,"sql2")=False Then
' File.Copy(File.DirAssets,"ambulance.db",File.DirInternal,"sql2")
'End If
'
' sql.Initialize(File.DirInternal,"sql2",True)
End Sub

Sub flp_ConnectionSuccess
Log("Connected to location provider")
End Sub

Sub flp_ConnectionFailed(ConnectionResult1 As Int)
Log("Failed to connect to location provider")
End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground

End Sub

Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
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 CheckLocationSettingStatus As ResumableSub
Dim f As LocationSettingsRequestBuilder
f.Initialize
f.AddLocationRequest(CreateLocationRequest)
flp.CheckLocationSettings(f.Build)
Wait For flp_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
Return LocationSettingsResult1
End Sub

Public Sub StartLocationUpdates
flp.RequestLocationUpdates(CreateLocationRequest)
End Sub

Private Sub flp_LocationChanged (Location1 As Location)
CallSub2(Main, "Location_Changed", Location1)
End Sub
Sub Service_Destroy

End Sub

Private Sub CreateLocationRequest As LocationRequest
Dim lr As LocationRequest
lr.Initialize
lr.SetInterval(0)
Return lr
End Sub

Codebase from map activity
B4X:
#Region   Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private gmap As GoogleMap
Private MapFragment1 As MapFragment
Private rp As RuntimePermissions
Private StateMessage As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
Wait For MapFragment1_Ready
gmap = MapFragment1.GetMap
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then

Log(Tracker.gpslat)
Log(Tracker.gpslong)

gmap.MyLocationEnabled = True
gmap.TrafficEnabled=True
'gmap.MyLocation.Initialize(Tracker.gpslat,Tracker.gpslong)

Do While gmap.MyLocation.IsInitialized = False
Log("Location not initialized... Sleep a while...")
Sleep(250)
Loop
If gmap.MyLocation.IsInitialized Then
Dim CameraPosition1 As CameraPosition 'CameraPosition1.Initialize2(gmap.MyLocation.Latitude, gmap.MyLocation.Longitude, gmap.CameraPosition.Zoom, 0, 0)
gmap.MapType=gmap.MAP_TYPE_NORMAL

CameraPosition1.Initialize(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude, 16)
gmap.AnimateCamera(CameraPosition1)

Sleep(2000)
CameraPosition1.Initialize(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude, 18)
gmap.AnimateCamera(CameraPosition1)

'Dim m As Marker
'Dim bitmap As Bitmap
'bitmap= LoadBitmapResize(File.DirAssets,"small-axe.png",30dip,30dip,True)

'gmap.AddMarker3(gmap.MyLocation.Latitude,gmap.MyLocation.Longitude,"",LoadBitmapResize(File.DirAssets,"small-axe.png",30dip,30dip,True))
End If

Else
Log("No permission!")
End If

End Sub

Sub Activity_Resume
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
StartService(Tracker)
Else
ToastMessageShow("No permission...", True)
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Update_Location(newloc As Location)
If newloc.IsInitialized Then
Dim CameraPosition1 As CameraPosition 'CameraPosition1.Initialize2(gmap.MyLocation.Latitude, gmap.MyLocation.Longitude, gmap.CameraPosition.Zoom, 0, 0)
CameraPosition1.Initialize(newloc.Latitude,newloc.Longitude, 17)
gmap.moveCamera(CameraPosition1)
End If
End Sub

Sub MapFragment1_Click (Point As LatLng)
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Starter.flp.IsConnected = False Then
SetState("Location provider not available")
End If
If Result Then
Dim rs As ResumableSub = CallSub(Starter, "CheckLocationSettingStatus")
Wait For (rs) Complete (SettingsResult As LocationSettingsResult)
Dim sc As StatusCodes
Select SettingsResult.GetLocationSettingsStatus.GetStatusCode
Case sc.SUCCESS
SettingsAreGood
Case sc.RESOLUTION_REQUIRED
SetState("RESOLUTION_REQUIRED")
SettingsResult.GetLocationSettingsStatus.StartResolutionDialog("srd")
Wait For srd_ResolutionDialogDismissed(LocationSettingsUpdated As Boolean)
If LocationSettingsUpdated Then
SettingsAreGood
Else
SetState("Not enabled")
End If
Case Else
SetState("Not available")
End Select
Else
SetState("No permission")
End If
End Sub

Sub SettingsAreGood
SetState("Location enabled - waiting for updates")
CallSub(Starter, "StartLocationUpdates")
End Sub


Public Sub Location_Changed (loc As Location)
'lblLocation.Text = $"$1.2{loc.Latitude} / $1.2{loc.Longitude}"$
End Sub


Sub SetState (msg As String)
StateMessage = msg
'lblState.Text = "State: " & msg
'Log(lblState.Text)
End Sub
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Solved the issue..... I was missing the below details

B4X:
#AdditionalJar: com.android.support:support-v4
#AdditionalJar: com.google.android.gms:play-services-location

Thanks @Erel and @DonManfred for your advice and guidance...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…