Geolocation?

547 views
Skip to first unread message

Aerik Sylvan

unread,
Apr 22, 2015, 1:42:04 PM4/22/15
to androi...@googlegroups.com
I'm trying to do standard javascript geolocation inside a html app - but the callback for the location is never called.  The exact same code works in a desktop browser, so I don't think the problem is my code.  Is geolocation not enabled?

Thanks,
Aerik

Steve Garman

unread,
Apr 22, 2015, 2:16:15 PM4/22/15
to androi...@googlegroups.com
This works for me.

Can you post your code?

<html>
<head>
    <meta name="viewport" content="width=device-width">
    <script src='file:///android_asset/app.js'></script>
</head>
    
<script>
    //Called after application is started.
    function OnStart()
    {
    //Create and start location sensor. 
    //(Achievable update rate is hardware specific) 
    loc = app.CreateLocator( "GPS" ); 
    loc.SetOnChange( loc_OnChange );  
    loc.SetRate( 10 ); //10 seconds. 
    loc.Start(); 
    }
 function loc_OnChange(data)
 {
     var s="Lat "+data.latitude+
        ", Lng "+data.longitude;
            app.ShowPopup(s);
 }
</script>

<style>
    body { background-color: #ffffff; }
    .hello 
    { 
        font-size: 42; 
        width: 100%;
        margin-top: 2em;
        text-align: center;
        color: blue;
    }
</style>

<body onload="app.Start()">

    <div class=hello> Hello World! </div>
    
</body>
</html>

Aerik Sylvan

unread,
Apr 22, 2015, 2:25:06 PM4/22/15
to androi...@googlegroups.com
Ah - I was trying to do regular javascript geolocation in an html app:

<html>
<head>
    <meta name="viewport" content="width=device-width">
</head>
<script>
    //Called after application is started.
    function OnStart()
    {
        //nothing
    }
</script>

<style>
body { background-color: #ffffff; }

</style>

<body onload="init()">

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function init(){
    x.innerHTML = "Initialized";
}

var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

function getLocation() {
    if (navigator.geolocation) {
        x.innerHTML = "Waiting...";
        navigator.geolocation.getCurrentPosition(showPosition,handleErr,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 handleErr(err){
    alert(err.message);
}
</script>
</body>
</html>

Steve Garman

unread,
Apr 22, 2015, 3:02:42 PM4/22/15
to androi...@googlegroups.com
I'm afraid I don't know whether the main WebView's WebChromeClient is customised to allow geolocation, I suspect not, though I would expect your "waiting..." message to change in that case.

If it is not, the nearest I can offer is

<html>
<head>
    <meta name="viewport" content="width=device-width">
    <script src='file:///android_asset/app.js'></script>
</head>
    
<script>
    //Called after application is started.
    function OnStart()
    {
        //nothing
    }
</script>
<style>
    body { background-color: #ffffff; }
</style>
<body onload="init()">
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function init(){
    app.Start();
    x.innerHTML = "Initialized";
}
var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};
function getLocation() {
   x.innerHTML = "Waiting...";
    //Create and start location sensor. 
    //(Achievable update rate is hardware specific) 
    loc = app.CreateLocator( "GPS" ); 
    loc.SetOnChange( showPosition );  
    loc.SetRate( 0 );
    loc.Start(); 
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.latitude + 
    "<br>Longitude: " + position.longitude; 
    loc.Stop()   

Aerik Sylvan

unread,
Apr 22, 2015, 5:38:39 PM4/22/15
to androi...@googlegroups.com
Thanks Steve,

I made a sort of wrapper so I can use my code in both a browser and in DroidScript... it's kind of ugly.  Iteresting - the callback for loc.SetOnChange seems to need to be a function declaration the is defined at "compile time" (not sure what to call it).  using a function expression doesn't work.


<html> 
<head> 
    <meta name="viewport" content="width=device-width"> 
    <script src='file:///android_asset/app.js'></script> 
</head> 
     
<script> 
    //Called after application is started. 
    function OnStart() 
    { 
        //nothing 
    } 
</script> 
<style> 
    body { background-color: #ffffff; } 
</style> 
<body onload="init()"> 
<p>Click the button to get your coordinates.</p> 
<button onclick="getLocation()">Try It</button> 
<p id="demo"></p> 
<script> 
var x = document.getElementById("demo");
var getLocation = function(){

    var options = { 
      enableHighAccuracy: true, 
      timeout: 5000, 
      maximumAge: 0 
    }; 
    navigator.geolocation.getCurrentPosition(showPosition,handleErr,options);
    x.innerHTML = "Waiting for JS location";
}
function init(){ 
    //see if we're running in DroidScript
    if(typeof(app)=="object" && typeof(app.CreateLocator) == "function"){
        var old = showPosition;
        //this doesn't work
        //showPosition = function(pos){ old(pos); alert("ran"); }
        //redefine function
        getLocation = function(){

            loc = app.CreateLocator( "GPS" );  
            loc.SetOnChange( showPosition );   
            loc.SetRate( 0 ); 
            loc.Start();
            x.innerHTML = "Waiting for DroidScript location";
        }
    }
    x.innerHTML = "Initialized"; 

function showPosition(position) { 
    lastLocationTime = Date.now();

Steve Garman

unread,
Apr 22, 2015, 10:42:06 PM4/22/15
to androi...@googlegroups.com
Thanks for posting your workaround, Aerik.

It is a bit frustrating not to be able to use function expressions as callbacks but the JavaScript->Java bridge only allows global functions as callbacks parameters.

Selo Soft

unread,
Apr 24, 2015, 3:42:27 PM4/24/15
to androi...@googlegroups.com

Does not work html code in Droidscript .
work in firefox and chrome .





<html>
<head>
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
</head>
<body>
<script language="Javascript">
document.write("Welcome to our visitors from "+geoplugin_city()+", "+geoplugin_countryName());
</script>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Reverse Geocoding</title>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}

function errorFunction(){
alert("Geocoder failed");
}

function initialize() {
geocoder = new google.maps.Geocoder();



}

function codeLatLng(lat, lng) {

var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
alert(results[0].formatted_address)
//find country name
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {

//there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
//this is the object you are looking for
city= results[0].address_components[i];
break;
}
}
}
//city data
alert(city.short_name + " " + city.long_name)


} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
<body onload=" city.short_name ()">
</body>
</html>
Message has been deleted

Ron Michel

unread,
Aug 19, 2016, 10:06:30 PM8/19/16
to DroidScript
Saddly i can not get any compass or altitude. I get my geo from google the above code does not seem to work. Geo bla undefined
S6 samsung compass is messed up but does produce. I get 0 in droidscript. Altitite sometimes works. I think something is wrong here.

Charles Wilt

unread,
Aug 20, 2016, 9:23:28 AM8/20/16
to DroidScript
Two things I discovered working with my note 3 compass. It is rotated 90 degress and you must spin it full circle for it to calibrate.

Ron Michel

unread,
Aug 20, 2016, 8:06:04 PM8/20/16
to DroidScript
yes, my samsung s6 is very bad compass. I got altitude to work by "GPS" only turned off my data and wifi and activated my gps in location settings, but it is very inaccurate... dang wanted to wiregrid an array based on walking around with my phone.

Charles Wilt

unread,
Aug 20, 2016, 10:17:29 PM8/20/16
to DroidScript
You need to give the GPS time to settle down. Check the accuracy outdoors and stopped. Outdoors you should see accuracy down to 3 meters. Civilian GPS struggles with vertical accuracy.
Reply all
Reply to author
Forward
0 new messages