Unable to get GPS coordinates using Javascript in Android browser

I am very new to android, java and javascript coding.

I am trying to get the current GPS coordinates and track them after that on an Android device. I am using webview and most of the code is written in javascript.

I searched many days several days and tried many different ways, but I can not get the GPS coordinates on the Android device. Please take a look at the code and see why I am not getting a GPS location.

A few points - 1. I downloaded and checked other applications for sensors and could see the current GPS coordinates (so that GPS works on the device). 2. When I run my code, I see the GPS search icon in the notification area, it continues to flash, but never fixes. 3. When I run only the HTML file with javascript in the browser for the laptop, it asks for permission to share, and then gives the coordinates, but the same file does not show anything in android.

Please take a look at the code below and help me figure this out. I have tried a lot and publish this as my last hope.

The manifest file has the following permissions -

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <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" /> 

The main Java code on Android is

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); webView = new WebView(this); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setLoadWithOverviewMode(true); webView.getSettings().setUseWideViewPort(true); webView.loadUrl("file:///android_asset/mymap.html"); setContentView(webView); } 

And the HTML file with javascript -

 <!DOCTYPE html> <html> <body onunload="OnUnload()" onload="getLocation()"> <p id="demo">Your coordinates:</p> <script> var watchID; var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { var options = {maximumAge:600000, timeout:100000, enableHighAccuracy: true}; watchID = navigator.geolocation.watchPosition(showPosition,showError,options); } else{x.innerHTML="Geolocation is not supported by this browser.";} } function showPosition(position) { x.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML="User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML="Location information is unavailable." break; case error.TIMEOUT: x.innerHTML="The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML="An unknown error occurred." break; } } function OnUnload() { alert ("The current document will be unloaded!"); navigator.geolocation.clearWatch(watchID); } </script> </body> </html> 

Thank you very much in advance, AXS

+6
source share
2 answers

Well, I did a further search and found a solution. I post these links here as an answer to my question for those who come here looking for answers.

I had to add these lines to my Java code before loading the URL, and then I could see the geo-coordinates.

  webView.getSettings().setGeolocationEnabled(true); webView.setWebChromeClient(new WebChromeClient() { public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { // callback.invoke(String origin, boolean allow, boolean remember); callback.invoke(origin, true, false); } }); 

For more information, follow these two links -

+7
source

On an Android 6.0 phone (not sure about other versions), this killed it for me in my build.gradle file:

 targetSdkVersion: 23 

reducing it to

 targetSdkVersion 21 

fixed my problem.

-1
source

Source: https://habr.com/ru/post/919421/


All Articles