Get geolocation on submit in Google Forms using Google Script
Maybe this works: https://www.youtube.com/watch?v=J93uww0vMFY
Tutorial on Geotagging (Geo-stamp and Time-stamp) google forms. Add info on Latitude, Longitude, and Address (Street name and number, city, state, zip code, and country) of a device submitting google forms. Linking user’s location within google forms. Google forms integration with google maps.
Links to download the scripts are given below:
Related videos on Youtube
prolifebel
Updated on June 04, 2022Comments
-
prolifebel almost 2 years
I have a Google Form where I want to get the user geolocation along with the inputs. Currently, I'm able to get it by making the user click on a url after he submits the answers. This is the code in Google Script that does it:
function doGet() { return HtmlService.createHtmlOutputFromFile("Index"); } function getLoc(value) { var destId = FormApp.getActiveForm().getDestinationId() ; var ss = SpreadsheetApp.openById(destId) ; var respSheet = ss.getSheets()[0] ; var data = respSheet.getDataRange().getValues() ; var headers = data[0] ; var numColumns = headers.length ; var numResponses = data.length; var c=value[0]; var d=value[1]; var e=c + "," + d ; if (respSheet.getRange(1, numColumns).getValue()=="GeoAddress") { respSheet.getRange(numResponses,numColumns-2).setValue(Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy HH:mm:ss")); respSheet.getRange(numResponses,numColumns-1).setValue(e); var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]); f= response.results[0].formatted_address; respSheet.getRange(numResponses,numColumns).setValue(f); } else if (respSheet.getRange(1,numColumns).getValue()!="GeoAddress") { respSheet.getRange(1,numColumns+1).setValue("GeoStamp"); respSheet.getRange(1,numColumns+2).setValue("GeoCode"); respSheet.getRange(1,numColumns+3).setValue("GeoAddress"); } }
And the Index.html file:
<!DOCTYPE html> <html> <script> (function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } })() function showPosition(position){ var a= position.coords.latitude; var b= position.coords.longitude; var c=[a,b] getPos(c) function getPos(value){ google.script.run.getLoc(value); } } </script> </html>
Instead of making the user click on a url, I want the geolocation to be automatically inputed into the response sheet as the user submits the form. I'm able to make the getLoc function run on a submit trigger, however the html function doesn't run. I believe it might be because the doGet function is already a trigger, but it requires a browser page to be opened in order to run. If that's the case, the ideal solution would be to redirect the browser to the Google Script url after the user submits it, but I can't seem to find a way to do it. Is this the correct approach and how can I make it work?
Thanks in advance!
-
alberto vielma over 4 yearsIf you create a WebApp form and in that way, you could get the geolocation values and then pass them with the form data submitted to the server-side Apps Script code. Do you like that approach?
-
Sourabh Choraria over 4 yearsDoes this answer your question? How to send geolocation along with inputs when submiting HTML form in Google Apps Script
-
Zesty Dragon over 3 yearswhy is geolocation coming incorrect
-
-
Alpi Murányi almost 4 yearsHi Wolkuz and thanks for the contribution! Some advice you might want to consider, for this and for future answers: 1. instead of simply linking in a tutorial, take the time and summarize it in your answer. The link can be made available as an addition to your exhaustive answer. 2. Please format links using the editor, so that what is visible is only a link title and not the whole link. Thanks again and keep on stackoverflowing!
-
Wolkuz almost 4 yearsGot it. I'm pretty new around here.
-
Denilson over 3 yearsIs there any way to choose a point on the map and capture the Lat and Long in google forms?
-
kiki over 3 yearsThis does nothing with me - doesn't write geo data to the sheet.