Weehooey
Weehooey

Reputation: 1008

Geolocation not working with HtmlService

I created the following two files:

code.gs

function doGet() {
  var html = HtmlService.createHtmlOutputFromFile('html.html');
  return html;
}

html.html

<html>
<body>
<p id="messaging">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Where am I</button>

<script>
  var message=document.getElementById("messaging");
  function getLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    } else {
      message.innerHTML="Geolocation is not supported.";
    }
  }

function showPosition(position) {
  message.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>

When I call the published URL, I get the expected message and button. Click the button and I get my failure message "Geolocation is not supported". If I save html.html in a file and open it in a browser it works as expected.

Any ideas?

Upvotes: 1

Views: 1403

Answers (4)

Waqar Ahmad
Waqar Ahmad

Reputation: 3730

I believe, Caja is culprit here. Could you run your code on Caja playground to check if the behavior is same. If same, you may open an issue in Caja Issue Tracker

To know more what Caja does with HtmlService, you may refer to this page.

Update Above answer is obsolete. Now one can access location using navigator.geolocation object available in browser.

Upvotes: 1

Sujay Phadke
Sujay Phadke

Reputation: 2196

As of 2016, geolocation works with HtmlService in IFRAME mode. Test it here: GAS-geolocation

Tested with Chrome and Firefox (desktop), both ok. Doesn't work with Safari (desktop) (the share location confirmation box doesn't popup. I hate Safari!)

Funny thing is, it works with Safari on iOS 9 but not the latest Chrome on iOS 9. (same issue, no confirmation popup)

Upvotes: 2

Daniel Boyles
Daniel Boyles

Reputation: 1

I just tried your code, as I'd like to do something with Google Apps Script in Google Sites.

It appears that GeoLocation is still not supported in HTMLService at this time, but I found a possible workaround for my specific need (i.e. in conjunction with Google Sites) that may help someone else too :

What does work, is to create a custom "Google Sites Gadget"

using the example code from tutorials point

I end up with a 'skeleton' XML file for my gadget as such :

<?xml version="1.0" encoding="UTF-8"?>
<Module>

<ModulePrefs title="GeoLocation"
            >
</ModulePrefs>

<Content type="html"><![CDATA[
    
    <form>
    <input type="button" onclick="getLocation();"
    value="Get Location"/>
    </form>
    
    <script>
    function showLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    alert("Latitude : " + latitude + " Longitude: " + longitude);
    }
    
    function errorHandler(err) {
    if(err.code == 1) {
    alert("Error: Access is denied!");
    }else if( err.code == 2) {
    alert("Error: Position is unavailable!");
    }
    }
    function getLocation(){
    
    if(navigator.geolocation){
    // timeout at 60000 milliseconds (60 seconds)
    var options = {timeout:60000};
    navigator.geolocation.getCurrentPosition(showLocation,
    errorHandler,
    options);
    }else{
    alert("Sorry, browser does not support geolocation!");
    }
    }
    </script>
]]></Content>
</Module>

Now instead of the alerts, I can pass the results to the Google Apps Scripts Web App as a URL parameter.

It's not ideal, but seems to work ok so far.

Upvotes: 0

Corey G
Corey G

Reputation: 7858

GeoLocation is not yet available in HtmlService

Upvotes: 1

Related Questions