Код: Выделить всё
< /code>
webview создан так: < /p>
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")
}
< /code>
разрешения, запрошенные с помощью: < /p>
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)
}
}
< /code>
js на веб-сайте: < /p>
function getLocation() {
navigator.geolocation.getCurrentPosition(showPosition, showError, {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
}
function showPosition(position) {
someElement.innerHTML="Latitude: " + position.coords.latitude +
"
Longitude: " + position.coords.longitude;
}
function showError(error) {
// ...
}
написано />https://uplink-geolocation.w3spaces.com/
Подробнее здесь: https://stackoverflow.com/questions/795 ... n-the-same