Click here to Skip to main content
14,446,960 members
Rate this:
Please Sign up or sign in to vote.
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 frameworks 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://helpburg.com/app/nirbhaya");

}


Android Mainfest 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.

What I have tried:

I have tried almost all stack overflow solutions including this and similar posts.
Posted
Comments
Kris Lantz 17-Jan-20 8:30am
   
Have you checked that the permission to access the user's location is being set properly? I recall a similar issue when I did geoservice stuff.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100