• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

HTML5 Geolocation not working in Android Webview

Debsmita Paul

Lurker
Jan 17, 2020
1
1
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 desktop browser and in mobile browser, but not in webview. I have tried some of the solutions on other forums, but nothing seems to solve this.

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):
Code:
$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):
Code:
public class MainActivity extends AppCompatActivity {

private WebView webView;

[USER=1021285]@override[/USER]
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");
}





Android Mainfest Permissions:
Code:
<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. Please help.
 
Last edited by a moderator:
  • Like
Reactions: racibennett

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones