简体   繁体   English

获取经纬度html js

[英]get latitude and longitude html js

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. 我想在mysql数据库中插入经度(long)和纬度(lat)。 found this code who gives me lon and lat 找到了这个给我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. 这段代码打印了经纬度,我想要的是在php中经纬度变长并在后面运行sql查询。

Using ajax you can save latitude and longitude into the database. 使用ajax可以将纬度和经度保存到数据库中。 For this update your script as given below. 对于此更新,您的脚本如下所示。

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
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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