2012-10-01 74 views
1

工作,我創建了以下兩個文件:地理位置不HtmlService

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> 

當我致電發佈的網址,我收到了預期的消息和按鈕。點擊按鈕,我得到我的失敗消息「不支持地理定位」。如果我將html.html保存在一個文件中,並在瀏覽器中打開它,它會按預期工作。

任何想法?

回答

1

,地理定位尚未HtmlService

+0

感謝科瑞。 PS你知道我專注於'尚'。 :-) – Weehooey

+0

沒有承諾,但我們正在努力添加內容 –

+0

自原始請求以來的更新?任何想法,如果/當它的GeoLocation將在HtmlService中可用? – wivku

1

我相信Caja是這裏的罪魁禍首。你可以在Caja playground上運行你的代碼來檢查行爲是否相同。如果相同,您可以在Caja Issue Tracker

中打開一個問題要了解更多Caja如何處理HtmlService,您可以參考此page

+0

顯然它不是一個Caja問題: [鏈接](https://code.google.com/p/google-caja/issues/detail?id=1815) – wivku

0

可我只是想你的代碼,因爲我喜歡做與谷歌Apps腳本東西在谷歌站點。

看來,地理定位仍然沒有HTMLService支持在這個時候,我發現了一個可能的解決方法爲我的特定需求(即在與谷歌的網站結合),可以幫助別人太:

什麼工作,是使用示例代碼tutorials point

我結束了我的小工具的「骨架」 XML文件作爲這樣創建自定義的「谷歌網站小工具」

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

現在不是警報,我可以將結果傳遞到谷歌Apps腳本Web應用程序爲URL parameter

這並不理想,但似乎迄今爲止工作正常。

+0

很酷。謝謝,我會檢查出來。 – Weehooey

1

截至2016年,地理位置在IFRAME模式下與HtmlService一起工作。在這裏測試:GAS-geolocation

測試Chrome和Firefox(桌面),都好。有趣的是,它適用於iOS 9上的Safari,但不適用於iOS 9上的最新Chrome。(不支持Safari瀏覽器(共享位置確認框不會彈出,我討厭Safari瀏覽器!同樣的問題,沒有確認彈出窗口)

+0

@sdx11不能做什麼?我只是測試它,它的工作原理。你在用什麼系統? –

+0

@ sdx11什麼是系統規格 - 操作系統和瀏覽器。 –