I have a very, very simple scenario and I am running into a very common issue (noted by many StackOverflow users over time). However, I have tried every proposed solution, and none of them seem to work. Can somebody please help to resolve this question once and for all?
I am building an Android app (for Android 7, let's assume) which contains a Webview which points to a website that implements very simple geolocation. (My latest, very stripped down version of the website simply displays the user's latitude and longitude.)
I can view the site fine (and it displays latitude/logitude perfectly) in Chrome. But when I run the app using Webview, nothing is displayed.
Over the past few days, I have tried seemingly every proposed solution (in MainActivity.java) to this issue, but none of the solutions seem to work.
I have added all permissions imaginable in AndroidManifest.xml.
What does one need to do to get this to work?
Surely, viewing a website with geolocation in an Android app with Webview should be fairly straightforward, right???
Edit: I've narrowed things down a bit. I've prompted the user for permissions at runtime, and I've confirmed that geolocation is being allowed. I've also confirmed that Javascript, in general, is being allowed by my app.
The key/core remaining issue seems to be this: The following does not appear to work in Webview.
navigator.geolocation.getCurrentPosition(success, error);
I don't know why, but that is the problem.
Ref:
Android webview error when asking for geolocation
The application does not have sufficient geolocation permissions
Location is accessed in Chrome, doesn't work in WebView
Android Ask for permission to use location within webview
Minimal geolocation webpage: (How to get this to display in Android Webview?)
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Webview Test</title>
</head>
<body>
<div id='output'></div>
<script>
function success(pos) {
var latitude = pos.coords.latitude;
var longitude = pos.coords.longitude;
document.getElementById('output').innerHTML = `Your current position is: Latitude : ${latitude} Longitude: ${longitude}`;
}
function error(err) {
console.warn('ERROR(${err.code}): ${err.message}');
}
navigator.geolocation.getCurrentPosition(success, error);
</script>
</body>
</html>