简体   繁体   中英

How to pass the Javascript location(Longitude & Latitude) value in to the Textbox value?

<body>
    <button onclick="getLocation()">Get coordinates</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 = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
        }
    </script>
    <input type="text" value="long,lati"></input>
</body>


I need to get the Latitude and longitude in the script into the text box without onclocking the button as per in the code.

<input type="text" value = "" id='loc'>
window.onload = function(){
 var x = document.getElementById('loc');
      function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.value = "Geolocation is not supported by this browser.";
        }
     }

     function showPosition(position) {
        x.value = "Latitude: " + position.coords.latitude +     ",Longitude: " + position.coords.longitude;
    }
getLocation();
};

Just Grab the input element and set it's value attr to the obtained lat & long . This in Not Great Solution, But it should do the job you needed (Without clicking the button).

        <body>
            <button>Get coordinates</button>
            <p id="demo"></p>
            <script>

                var x = document.getElementById("demo");

               getLocation();

                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>
            <input type="text" value="long,lati"></input>
        </body>

To get latitude and longitude in the textbox you need to get the id of textbox and pass the value of lat long there

Here is a Jsfiddle http://jsfiddle.net/2b4n03k1/

var x = document.getElementById("demo");


function showPositionInInput(position) {
 var input = document.getElementById("location");
 input.value = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}

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

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

getLocation()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM