Search code examples
javascriptgoogle-apps

Geolocation to imput field and form submition (google script)


Hi i have this link to get the current user position by GPS. Its part of a form script i made getting diferent codes from different places.

when pressing the button it shows a paragraph with coordinates.

<button onclick="getLocation()">Obtener coordenadas de GPS del movil</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
x.innerHTML = position.coords.latitude + "," + position.coords.longitude;
}

</script>

But i want to populate this field in the form:

<div id="formDiv">
<!-- Form div will be hidden after form submission -->
<form id="myForm">
<br/>
Ubicacion: <input name="ubicacion" type="text" size="64" /><br/>

anyone knows if its posible? tryed several things, but none working, till i dont know much of javascript.

this is the form: site

At pressing "obtener coordenadas del GPS" it should fill the "Ubicacion" imput field.


Solution

  • Got it! i should have used another formula: .value not .innerHTML

     <button onclick="getLocation()">Obtener coordenadas de GPS del movil</button><br/>
    <script>
     function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        document.getElementById("ub").value = "Geolocation is not supported by this browser.";
           }
    }
    function showPosition(position) {
    document.getElementById("ub").value = 
         position.coords.latitude + "," +   position.coords.longitude;
    }
    </script>
    

    seems that also using var x = doesnt work

     Ubicacion: <input name="ubicacion" type="text" size="64" /><br/>