Android Question Runtime Permissions (Android 6.0+ Permissions) Maps

tracklocator

Member
Licensed User
This is my first question just started in the development of Apps with B4A update my version 6.50 and try to make the example (https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/) but I can not make it work.

my code is:
B4X:
#Region  Project Attributes
   #ApplicationLabel: Maps Example
   #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

'Activity module
Sub Process_Globals
    Dim rp As RuntimePermissions
End Sub

Sub Globals
    Private gmap As GoogleMap
    Private MapFragment1 As MapFragment
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Please install Google Play Services.", True)
    End If
End Sub

Sub MapFragment1_Ready
    gmap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Dim m1 As Marker = gmap.AddMarker(10, 30, "test")
    m1.Snippet = "This is the snippet"
End Sub


Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
        If Result Then
            gmap.MyLocationEnabled = Result
            End If
    End If
End Sub

This is the error
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:196)
at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:387)
at zu.a:)com.google.android.gms.DynamiteModulesB:82)
at maps.ad.t$5.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
at maps.ad.t.c(Unknown Source)
at xj.onTransact:)com.google.android.gms.DynamiteModulesB:274)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.setMyLocationEnabled(Unknown Source)
at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(Unknown Source)
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:183)
... 12 more

I have tried different ways but I still do not make it work.

I hope you can help me

Thanks for your time.
 

DonManfred

Expert
Licensed User
Longtime User
Please upload your project (like i already told in the other thread ;-))

Additionally you should follow this tutorial on how to add google Maps
https://www.b4x.com/android/forum/threads/google-maps.63930/#content
If you carefully follow this instructions then your app should have the needed permission.

I guess you did not follow the instructions carefully.
 
Upvote 0

tracklocator

Member
Licensed User
Please upload your project (like i already told in the other thread ;-))

Additionally you should follow this tutorial on how to add google Maps
https://www.b4x.com/android/forum/threads/google-maps.63930/#content
If you carefully follow this instructions then your app should have the needed permission.

I guess you did not follow the instructions carefully.

thanks for your time.
I followed the instructions to the letter but I can not make it work the code.

regards
 

Attachments

  • mapexample.zip
    8.2 KB · Views: 368
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Dim rp As RuntimePermissions
End Sub
Sub Globals
    Private gmap As GoogleMap
    Private MapFragment1 As MapFragment
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Please install Google Play Services.", True)
    End If
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub


Sub MapFragment1_Ready
    Log($"MapFragment1_Ready"$)
    'gmap = MapFragment1.GetMap
    'Dim m1 As Marker = gmap.AddMarker(10, 30, "test")
    'm1.Snippet = "This is the snippet"
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    Log($"Activity_PermissionResult(${Permission},${Result})"$)
    If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
        If Result Then
            gmap = MapFragment1.GetMap
            gmap.MyLocationEnabled = Result
        End If
    End If
End Sub
 

Attachments

  • maps.zip
    8.5 KB · Views: 391
Upvote 0

tracklocator

Member
Licensed User
B4X:
Sub Process_Globals
    Dim rp As RuntimePermissions
End Sub
Sub Globals
    Private gmap As GoogleMap
    Private MapFragment1 As MapFragment
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If MapFragment1.IsGooglePlayServicesAvailable = False Then
        ToastMessageShow("Please install Google Play Services.", True)
    End If
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub


Sub MapFragment1_Ready
    Log($"MapFragment1_Ready"$)
    'gmap = MapFragment1.GetMap
    'Dim m1 As Marker = gmap.AddMarker(10, 30, "test")
    'm1.Snippet = "This is the snippet"
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    Log($"Activity_PermissionResult(${Permission},${Result})"$)
    If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
        If Result Then
            gmap = MapFragment1.GetMap
            gmap.MyLocationEnabled = Result
        End If
    End If
End Sub




Thank you so much for your time and help.
This is very helpful to me since I was stuck for days trying.
 
Upvote 0

Angel Garcia

Member
Licensed User
Thank you so much for your time and help.
This is very helpful to me since I was stuck for days trying.

Hi,
How did you resolved this man?
I'm facing the same issue with no success.
i have a MapFragment that loads immediately on a activity, i used the code of Don but same error happens.
Apparently everything loads too fast.
Im targeting SDK:26, MinSDK: 14
In the first attempt (where no permission has been granted):
Logger connected to: samsung SM-G955F
--------- beginning of main
--------- beginning of system
Copying updated assets files (31)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
** Activity (main) Resume **
HandleJob: 2
** Activity (main) Pause, UserClosed = false **
** Activity (elmenu) Create, isFirst = true **
** Activity (elmenu) Resume **
java.lang.RuntimeException: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:196)
at com.google.android.gms.maps.zzab.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:507)
at er.b:)com.google.android.gms.DynamiteModulesB@11951440:20)
at com.google.android.gms.maps.internal.bf.a:)com.google.android.gms.DynamiteModulesB@11951440:5)
at com.google.maps.api.android.lib6.impl.bc.run:)com.google.android.gms.DynamiteModulesB@11951440:5)
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:6776)
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)
Caused by: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
at com.google.maps.api.android.lib6.impl.ba.c:)com.google.android.gms.DynamiteModulesB@11951440:684)
at com.google.android.gms.maps.internal.k.onTransact:)com.google.android.gms.DynamiteModulesB@11951440:141)
at android.os.Binder.transact(Binder.java:507)
at com.google.android.gms.internal.zzeu.zzb(Unknown Source)
at com.google.android.gms.maps.internal.zzg.setMyLocationEnabled(Unknown Source)
at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(Unknown Source)
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:183)
... 13 more
--------- beginning of crash

'second run:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (auth_tokenavailable)
** Activity (elmenu) Create, isFirst = true **
** Activity (elmenu) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **

And something funny happens: both the "not responding App" message and behind it the "Allow access to this device location" request both arises at the same time.
It crashes and raises the permission request.

On the second run (when permission now is granted), It loads without problem, now that obviously the permission is granted.
How you fixed this?
Many thanks in advance
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0
Top