Get geolocation on submit in Google Forms using Google Script

17,686

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:

  1. Code.gs : https://www.youtube.com/redirect?v=J93uww0vMFY&event=video_description&q=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1D4vTzUGZAf3_ZDVMeW760i1KoZCn37un&redir_token=VQ2rLeQvyQea-mq9_kGhh_Kxihd8MTU5MTgxOTM2OEAxNTkxNzMyOTY4

  2. Index.html : https://www.youtube.com/redirect?v=J93uww0vMFY&event=video_description&q=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1mYZGYNrXNOxUD8DlDmRdBajy1nc3FKtw&redir_token=VQ2rLeQvyQea-mq9_kGhh_Kxihd8MTU5MTgxOTM2OEAxNTkxNzMyOTY4

Share:
17,686

Related videos on Youtube

prolifebel
Author by

prolifebel

Updated on June 04, 2022

Comments

  • prolifebel
    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!

  • Alpi Murányi
    Alpi Murányi almost 4 years
    Hi 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
    Wolkuz almost 4 years
    Got it. I'm pretty new around here.
  • Denilson
    Denilson over 3 years
    Is there any way to choose a point on the map and capture the Lat and Long in google forms?
  • kiki
    kiki over 3 years
    This does nothing with me - doesn't write geo data to the sheet.