Geolocation cordova and javascript

1

I would like to be able to take the value of the variable lat out of the function, to put it in a form. In this case, I just wanted to take out an alert (lat), but it does not work. Or it shows everything or nothing, but only lat no.

<html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">



        navigator.geolocation.getCurrentPosition(onSuccess, onError);


    // onSuccess Geolocation
    //
    function onSuccess(position) {
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                                    'Longitude: '          + position.coords.longitude             + '<br />';


    var lat = position.coords.latitude;
    var lon = position.coords.longitude;

                           }

    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    </script>
  </head>
  <body>
    <p id="geolocation">Finding geolocation...</p>
    <script>alert (lat);</script>
  </body>
</html>
    
asked by Carlos Julian 31.07.2017 в 10:50
source

1 answer

0

If you want to get the values of latitude and length in a form, do it like this:

In your onSuccess :

  function onSuccess(position) {
    document.getElementById("lat").value = position.coords.latitude;
    document.getElementById("long").value = position.coords.longitude;
  }

This implies that in your view you should have something like this:

<form action="#">
     Latitude:<br>
     <input type="text" id="lat" value="">
     Longitude:<br>
     <input type="text" id="long" value="">
</form>

By doing this you can assign the Latitude and Longitude values in your form fields.

    
answered by 01.08.2017 в 04:54