vlad
vlad

Reputation: 1035

Obtain google maps coordinates from user

I am not even sure that this is possible, but here it is.

I want user to fill the form, and once he goes to the field

<label for="pos">My possition</label>          
<input type="email" id="pos" name="clients_position">

I want to request his position and receive from him a Google Maps coordinate in such format "40.417035, -3.685389", ideally I want them automatically added to the input field.

Just did first basic course on JS in T3H and I am totally lost. HTML and CSS is a walk in a park compared to this, so I need some directions.

Upvotes: 0

Views: 146

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

the simplest way for obtain the coordinates of an user is use html 5 geolocation this is a sample

    <!DOCTYPE html>
    <html>
      <body>

      <p>Click the button to get  the coordinates.</p>

      <button onclick="getLocation()">Get coordinates </button>

      <p id="my_sample"></p>

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

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

      function showPosition(position) {
          x.innerHTML = "Latitude: " + position.coords.latitude + 
          "<br>Longitude: " + position.coords.longitude;  
      }
      </script>

      </body>
    </html>

Upvotes: 1

Related Questions