- Home /
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);
}
}
@Tamlou did you find a solution to this? I'm running into the same issue. Menu scene > Mapbox map. Map is blank and grey, restart app and map pops up just fine. Thanks in advance.
Answer by xxmariofer · Oct 28, 2020 at 01:43 PM
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.
Hi, Thanks for the reply. $$anonymous$$y Android $$anonymous$$anifest includes both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION It's also worth mentioning that I have a 'Home' scene, then the scene with the $$anonymous$$ap is an AssetBundle - this is when it asks for permission for location. I don't know if it being an AssetBundle affects when the permission is asked
Answer by Cubixrube · Apr 15, 2021 at 05:42 AM
I seem to have found a solution here: https://docs.unity3d.com/Manual/android-RequestingPermissions.html 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.
Your answer
Follow this Question
Related Questions
Get Android devices current country location 0 Answers
GPS Locate.permission Android? 0 Answers
GPS doesn't work in AGPS mode 0 Answers
How to import GPS location coordinates from Android device? 4 Answers
Location Service on iOS 1 Answer