Ask for Location permission at the start of the App

I am using Mapbox which shows the Users location on a Map. When I open the scene on an Android device (Samsung S8) it requests the permission but when you press Allow, it loads a grey screen. The rest of the assets are there (GUI) but it looks like the Map hasn’t initialized. if I close the App and reopen it, the Map now works.
Testing it on iOS, it all works fine.

I thought that if I could get the permission to come up when you first open the App - as opposed to when you open the Map scene - that it would have time for the Map to initialize.

Does anyone know how I can solve this issue?

I have this script on the Camera in the Map scene:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Locating : MonoBehaviour
{
    private Vector2 targetCoordinates;
    private Vector2 deviceCoordinates;
    public static float lat;
    public static float longi;
    private bool ready = false;

    private void Start(){
        StartCoroutine(StartLocationService());
        StartCoroutine(updateGPS());
    }
    public IEnumerator StartLocationService(){
        if (!Input.location.isEnabledByUser){
            Debug.Log("User has not enabled GPS");
            yield break;
        }
        Input.location.Start();
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0){
            yield return new WaitForSeconds(1);
            maxWait--;
        }
        if (maxWait < 1){
            Debug.Log("Timed out");
            yield break;
        }

        if (Input.location.status == LocationServiceStatus.Failed){
            Debug.Log("Unable to determine device location");
            yield break;
        }
        else{
            longi = Input.location.lastData.longitude;
            lat = Input.location.lastData.latitude;
        }
        ready = true;
    }
    public IEnumerator updateGPS(){
        if (!Input.location.isEnabledByUser){
            Debug.Log("User has not enabled GPS");
            yield break;
        }
        float UPDATE_TIME = 1f;
        WaitForSeconds updateTime = new WaitForSeconds(UPDATE_TIME);
        while (true){
            /* mapScript.Refresh();*/
            longi = Input.location.lastData.longitude;
            lat = Input.location.lastData.latitude;
            SetLocation();
            yield return updateTime;
        }
    }
    void SetLocation(){
        // radius.transform.position = new Vector3(0, 0, 6);
    }
}

If you add the ACCESS_FINE_LOCATION permision to the AndroidManifest.xml it should ask request the permision at the start of the app if i am not wrong.

I seem to have found a solution here: Unity - Manual: Request runtime permissions Grabbed the code and changed all cases of “Permission.Microphone” to “Permission.FineLocation”. It now asks for permission in my menu scene which allows Mapbox to load the map properly when you go to the map scene. Tested on several galaxy and pixel devices.

I have used following plugins to access location in Unity for iOS and Android platform. Plugins also adds maps inside unity . Developer is also very helpful. Links :

iOS : Location Manager Plugin For iOS | Integration | Unity Asset Store

Android : Location Manager for Android | Integration | Unity Asset Store

I had issues with this recently and I discovered that my permissions request was not stopping the thread where I had wanted it to. You can use the class PermissionCallbacks to be certain about when the user finishes accepting them

var cb = new PermissionCallbacks();
cb.PermissionDenied += (s) =>
{
    Debug.LogErrorFormat("User denied permission: {0}", s);
};
cb.PermissionGranted += (s) =>
{
    Debug.LogErrorFormat("User granted permission: {0}", s);
};
cb.PermissionDeniedAndDontAskAgain += (s) =>
{
    Debug.LogErrorFormat("User Denied permission, dont ask again: {0}", s);
};

Permission.RequestUserPermission(Permission.FineLocation, cb);

Then you can call other functions within those callbacks to go down the correct path depending on what the user has chosen. Simply requesting permissions did not halt the thread correctly.