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