得到纬度和经度html js

时间:2021-01-12 16:02:58

i have to do something and i have already a part of work. I want to insert longitude(long) and latitude(lat) in mysql database. found this code who gives me lon and lat

我必须做点什么,而且我已经完成了一部分工作。我想在mysql数据库中插入经度(长)和纬度(纬度)。发现这个代码给了我lon和lat

<!DOCTYPE html>
<html>
<body>

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

<button onclick="getLocation()">Try It</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>

</body>

This code print long and lat, that i want is to get long and lat in php and run sql query behind.

这段代码打印long和lat,我想要的是在php中运行并运行sql查询。

1 个解决方案

#1


1  

Using ajax you can save latitude and longitude into the database. For this update your script as given below.

使用ajax,您可以将纬度和经度保存到数据库中。对于此更新,您的脚本如下所示。

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

    saveData(position.coords.latitude,position.coords.longitude);

}

function saveData(latitude,longitude){
        $.ajax({
        url: 'save_data.php', // Script to save latitude and longitude
        type: 'GET', 
        data: { latitude : latitude, longitude : longitude },
        dataType: "json",     
        success: function(result) {
            //Return result
        }
    });
}

#1


1  

Using ajax you can save latitude and longitude into the database. For this update your script as given below.

使用ajax,您可以将纬度和经度保存到数据库中。对于此更新,您的脚本如下所示。

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

    saveData(position.coords.latitude,position.coords.longitude);

}

function saveData(latitude,longitude){
        $.ajax({
        url: 'save_data.php', // Script to save latitude and longitude
        type: 'GET', 
        data: { latitude : latitude, longitude : longitude },
        dataType: "json",     
        success: function(result) {
            //Return result
        }
    });
}