Skip to content
webtoapkfree

Why does geolocation not work in my Android WebView app?

WebView geolocation fails when the app lacks location permission, never answers the WebChromeClient prompt, or loads the page over plain HTTP.

GuidesNookesh Karri

Light cover card with the title 'Why does geolocation not work in my Android WebView app?' in dark text and a green accent bar

WebView geolocation fails for one of three reasons: the app has no Android location permission, your WebChromeClient never answers the page’s location prompt, or the page loads over plain HTTP. A WebView needs both permission layers, Android’s and the web page’s, before navigator.geolocation returns anything. Fix all three and the same code that works in Chrome works in your app.

Why WebView geolocation works in Chrome but not in your app

Chrome handles location permission for you, and a bare WebView doesn’t. In a WebView, your app plays the browser’s role. When a page calls getCurrentPosition(), WebView asks your app, and your app must reply.

The WebSettings reference lists two requirements. The app must have permission to access the device location, and it must implement onGeolocationPermissionsShowPrompt to hear when a page wants location. Skip either one and the page gets nothing.

This catches people who did half the work. One developer thread describes a locate button that worked in browsers but showed “User denied Geolocation” in the WebView console. The app had already requested location permission at runtime.

What a working WebView geolocation setup needs

A working setup has four parts, and each one fails silently when missing:

  1. Manifest permissions. Android’s location permissions guide says to always include ACCESS_COARSE_LOCATION. Add ACCESS_FINE_LOCATION only if you need precise location.
  2. The user’s grant. Declaring the permission isn’t enough. Ask for it in the app before the page needs it.
  3. JavaScript on. setJavaScriptEnabled defaults to false, so a page script can’t run at all until you turn it on.
  4. A prompt handler. A WebChromeClient that answers the page’s location request.

setGeolocationEnabled defaults to true, so you rarely need to touch it. If an old tutorial tells you to call setGeolocationDatabasePath, drop it: the reference says it’s deprecated and has no effect.

How to answer the WebChromeClient location prompt

The prompt handler is where most WebView geolocation bugs live. The WebChromeClient reference says onGeolocationPermissionsShowPrompt fires when a page wants location and no permission state is set for that origin. Your app “should invoke the specified callback with the desired permission state.”

Here’s a minimal Kotlin version:

webView.settings.javaScriptEnabled = true
webView.webChromeClient = object : WebChromeClient() {
    override fun onGeolocationPermissionsShowPrompt(
        origin: String,
        callback: GeolocationPermissions.Callback
    ) {
        val granted = ContextCompat.checkSelfPermission(
            this@MainActivity, Manifest.permission.ACCESS_COARSE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED
        callback.invoke(origin, granted, false)
    }
}

The Callback reference explains the arguments. allow says whether this origin may use the Geolocation API. retain says whether the answer outlives the page being shown.

I’d pass false for retain while testing. A remembered “deny” from a bad first run is hard to spot, and a fresh prompt per page load shows you exactly what’s happening. Check the origin too, so a third-party page inside your app can’t read location on your users’ grant.

If the user leaves the page mid-request, WebView calls onGeolocationPermissionsHidePrompt. Close any dialog you showed there.

Why HTTP pages never get a location

On HTTP, WebView geolocation is blocked before your code runs. The WebChromeClient reference says that for apps targeting Android N and later, the prompt “is only called for requests originating from secure origins such as https. On non-secure origins geolocation requests are automatically denied.”

The web platform says the same. The MDN page for getCurrentPosition marks it as available only in secure contexts. Chrome has required this since Chrome 50, counting top-level HTTPS pages and localhost as secure. If you also load HTTP pages, you’ll likely hit ERR_CLEARTEXT_NOT_PERMITTED first.

Match the symptom to the cause

Geolocation failures in a WebView look different depending on which layer broke, and the symptom points to the fix:

What you see Likely cause Fix
getCurrentPosition() never returns Prompt handler never calls the callback Call callback.invoke() on every path
Error code 1 on an HTTP page Non-secure origin, auto-denied Serve the page over HTTPS
Error code 1 on an HTTPS page No Android permission, or allow was false Declare and request location, then pass granted
Only a rough position User chose approximate, or only coarse declared Add ACCESS_FINE_LOCATION if you need precision
Nothing happens at all JavaScript disabled setJavaScriptEnabled(true)

The hang is the sneaky one. MDN says the timeout option defaults to Infinity, so getCurrentPosition() “won’t return until the position is available.” If your handler never answers, the page waits forever with no error. Always pass a timeout in your own pages.

Error code 1 is ambiguous. The Chrome post says the secure-context block returns code 1, the same “Permission Denied Error” you get when a user or the system denies access. Check the page’s origin before you chase permissions. Remote debugging helps here; see how to debug a WebView with Chrome DevTools.

The approximate case surprises people. Android’s guide says that if the user grants approximate location, your app gets only approximate location “regardless of which location permissions your app declares.”

Geolocation in apps built with WebToAPK Free

WebToAPK Free packages a hosted HTTPS website as a WebView app, and Website sections require a complete HTTPS address, so the HTTP failure above doesn’t apply. The limitation: the builder doesn’t promise that every site behaves as in Chrome. Its website-to-APK guide notes that sign-in, downloads, permissions and external links can work differently in a WebView.

If your site depends on location, test it on a real phone before you publish. The same goes for camera and microphone access, which follows a similar permission model. And if your app reads location at all, declare it in Google Play’s Data safety form.

FAQ

Why does getCurrentPosition never return in my WebView?

Usually nobody answered the permission prompt. WebView asks your app through onGeolocationPermissionsShowPrompt, and if you never call the callback, the request waits. The timeout option defaults to Infinity, so the page waits too.

Do I need location permission in the manifest for a WebView app?

Yes. Android’s WebSettings reference says the app must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION, and the user must grant it, before WebView can pass a location to the page.

Does WebView geolocation work on HTTP pages?

No. For apps targeting Android N and later, requests from non-secure origins are denied automatically, and the prompt callback is never called. Serve the page over HTTPS.

Why do I only get an approximate location?

The user picked approximate location in the permission dialog, or you declared only ACCESS_COARSE_LOCATION. Android then gives your app approximate location no matter what you declared.

Sources