1

The target is to embed a Google Apps Script gadget into my Google site. The function of this script is to show a google map, determin the location of the client and center the map to the found location.

The relevant command for getting the position is navigator.geolocation.getCurrentPosition.

The issue is that the security layers block the execution of this command when used with Chrome. (It works in Firefox and Safari. Also when I use "test web for your lates code", it works as intended).

enter image description here

Debugging the browser reveals the following error message: "Geolocation access has been blocked because of a Feature Policy applied to the current document. See (here comes a link) for more detail."

Following the explanations in the offered link, it results in the suggestion to apply a instruction for geolocation in the iframe command like:

<iframe src="https://example.com" allow="geolocation"></iframe>

Now here comes my problem: Since I can only use the functions in the Google Site editor to embed a GAS gadget, I have no option to modify an iframe instruction with such a parameter. Therefore, I do not know how I can pass this parameter into the web site.

The question is: Is there a method at all? If yes, what is the correct way to do it?

I have investigated a lot and also tried to add parameters to the doGet function in the sense of:

function doGet() { 
   return HtmlService.createHtmlOutputFromFile('pmtest') 
    .setSandboxMode(HtmlService.SandboxMode.IFRAME) 
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);}

But this did not make a change.

Here now my code: code.gs :

function doGet() { return HtmlService.createHtmlOutputFromFile('pmtest') }

html code (pmtest) :

<!DOCTYPE html>
<html>

<script
src="https://maps.googleapis.com/maps/api/js?key=...here is my 
key....&libraries=places&callback=initMap">
</script>

<script>
var myCenter=new google.maps.LatLng(51.158742,6.7170850);

function initialize()
{

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        alert('success');
        var myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        alert('myLatlng : ' + myLatlng);
        myCenter = myLatlng;
        CenterMap();
        marker.setPosition(myCenter);
    }, function(error) {
       alert('error');
    });
} 

var mapProp = {
center: myCenter, zoom:18, gestureHandling: 'greedy', tilt: 0, draggableCursor: 'default',
mapTypeId: google.maps.MapTypeId.SATELLITE
};

var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var marker = new google.maps.Marker({ position: myCenter});
marker.setMap(map);
function CenterMap(){ map.panTo(myCenter);}
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

<body>
 <div id="googleMap" style="width:1200px;height:700px;"></div>
</body>
</html>

In case you want to open the test page, here is the link: https://sites.google.com/site/pmtest4711/ When the geolocation command could be executed, a message will appear with "success" and a second with the found position. The google map then shows this location (wherever you are.).

Any good hint would be appreciated.

  • Welcome. Google Sites has a lot of restrictions, I'm not sure that this is possible. Have you find another Google Site that use this feature? – Rubén Dec 20 '18 at 18:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.