Search code examples
androidwebviewgeolocationandroid-webview

Getting "Location information is Unavailable" error in navigator.geolocation.getCurrentPosition() in Android Webview


I am currently building a webview application for android using android studio. The website that I am trying to wrap within a webview implements a simple geolocation system that tracks the current location of the user and stores latitude and longitude.

I am able to get the desired result in the desktop's browser and in the mobile browser, but not in webview. I have tried some of the solutions on other forums, but nothing seems to solve this.

I have also tried this. I tried the solutions, It didn't work.

The project is built in AngularJS 1.4.8. I am not using any framework like ionic or framework7. To get the location, I have used HTML5 Geolocation.

My AngularJS Code to get current location (works fine in browser):

$scope.getLocation = function () {
        if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(showPosition, showError);
                //alert("work");

            } else { 
                x.innerHTML = "Geolocation is not supported by this browser.";
                alert("Geolocation is not supported by this browser");
            }
        }

         function showPosition(position) {
            alert("position");
               var latitude_val ,longitude_val ;
               latitude_val = position.coords.latitude ;
               longitude_val = position.coords.longitude ;
               alert("lat"+latitude_val + "lon"+longitude_val)
            }

            function showError(error) {
                var error;
                switch(error.code) {
                    case error.PERMISSION_DENIED:
                    error = "User denied the request for Geolocation."
                    break;
                    case error.POSITION_UNAVAILABLE:
                    error = "Location information is unavailable."
                    break;
                    case error.TIMEOUT:
                    error = "The request to get user location timed out."
                    break;
                    case error.UNKNOWN_ERROR:
                    error = "An unknown error occurred."
                    break;
                }

                alert(error);
            }

Android Studio code (MainAcivity.java)

public class MainActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.activity_main);

    webView =(WebView)findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient());

    WebSettings webSettings = webView.getSettings();

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setGeolocationEnabled(true);
    webView.getSettings().setGeolocationDatabasePath( getFilesDir().getPath() );

    // HTML5 API flags
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);


    webView.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });



    webView.loadUrl("https://my.url");

}


@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    }
    else {
        super.onBackPressed();
    }
}

Android Manifest Permissions

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

It seems like the position parameter is always null but I cannot figure out why.


Solution

  • As per the documentation, from Android 6 (API 23) or higher, you need to implement the run time permissions.

    Here are some answers that will help you, with how to provide run-time permission. If you want to ask for multiple permission at the same time, you can follow these answers.