Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get GPS coordinates using Javascript on browser in Android

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

like image 441
axs Avatar asked Jul 01 '12 09:07

axs


People also ask

Can JavaScript get location?

The JavaScript Geolocation API provides access to geographical location data associated with a user's device. This can be determined using GPS, WIFI, IP Geolocation and so on. To protect the user's privacy, it requests permission to locate the device.

How do I access geolocation in my browser?

The Geolocation API is accessed via a call to navigator. geolocation ; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).

Can I get GPS coordinates on my phone?

Google Maps For Android users, Google Maps is the simplest way to either enter latitude and longitude coordinates or to find out your current coordinates. If you want to find your current coordinates, zoom into your location and hold down on the touchpad screen in the map.


1 Answers

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 -

  • android webview geolocation
  • Android: Using html5 to determine geolocation in webview with javascript api
like image 106
axs Avatar answered Oct 23 '22 04:10

axs