0

I have a barebones Android App with a WebView which loads a web page that wants to use geolocation.

All required permissions requested and granted. The web page gets the geolocation, but gets stuck on it.

Changing the location in the Android Emulator does not get reflected in the WebView when I request a location refresh.

Switching to Chrome to refresh the geolocation and then switching back to the app makes the new location appear in the WebView.

Even uninstalling the app doesn't make it pick up the new location. It keeps getting the old location.

What can I do to make the WebView get the new location without switching away to Chrome? I'd rather not waste time doing the extra steps all the time.

A real device seems to be updating fine without the extra steps.

Both the emulator and the real device are running Android 14.

Permissions:

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

WebView instantiated like this:

            WebView(context).apply {
                settings.setJavaScriptEnabled(true)
                settings.setGeolocationEnabled(true)
                setWebChromeClient(object : WebChromeClient() {
                    override fun onGeolocationPermissionsShowPrompt(origin : String, callback : GeolocationPermissions.Callback) {
                        callback.invoke(origin, true, false);
                    }
                })
                webViewClient = WebViewClient()
                loadUrl("https://mywebsite.example.com")
            }

Permissions requested with:

    private val requestPermissionLauncher =
        registerForActivityResult(
            ActivityResultContracts.RequestPermission()
        ) { isGranted: Boolean ->
            if (!isGranted) {
                Toast.makeText(this,"Location permission not granted",Toast.LENGTH_LONG).show()
            }
        }

//...

        when (PackageManager.PERMISSION_GRANTED) {
            ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) -> {
            }
            else -> {
                requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
            }
        }

JS on a website:

function getLocation() {
    navigator.geolocation.getCurrentPosition(showPosition, showError, {
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
    });
}

function showPosition(position) {
    someElement.innerHTML="Latitude: " + position.coords.latitude +
        "<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
   // ...
}

I also have an HTML button to call getLocation on demand.

At the time of this posting, I have uploaded the Android source code here:

https://gitlab.com/nucleware/toy-box/android-webview-geolocator/-/tree/stack-overflow-question?ref_type=tags#

and the corresponding web page here:

https://uplink-geolocation.w3spaces.com/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.