6

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

I am trying to get current GPS co-ordinates and track it thereafter on an Android device. I am using webview and the majority of the code is written in javascript.

I have searched a lot for a few days and tried many different ways but I am unable to get GPS co-ordinates on the android device. Please have a look at the code and hel pme figure out why I am not getting the GPS location.

A few things to point are - 1. I downloaded and checked other sensor apps and can see the current GPS co-ordinates being shown (so the GPS is working on the device). 2. When I run my code, I see the GPS searching icon in the notification area, it keeps blinking but never fixes. 3. When I run only the HTML file with the javascript on a laptop browser, it ask permission to share location and then it gives the co-ordinates, however the same file does not show anything in android.

Please have a look at the code below and help me figure this out. I have tried a lot and am posting 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 in Android has -

@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 the javascript is -

<!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>

Thanks a lot in advance, axs

2 Answers 2

7

Well I did further search and found a solution. I am posting these links here as answer to my question for anyone who comes till here looking for answers.

I had to add these lines in my java code before loading the url and then I was able to 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 -

Sign up to request clarification or add additional context in comments.

1 Comment

whe I use the webView.setWebChromeClient(new WebChromeClient() {... the app open the chrome app on my phone...why?
-1

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

targetSdkVersion: 23

reducing that to

targetSdkVersion 21

fixed my issue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.