0

嗨,我正在尝试创建一个商店定位器,我想将用户的纬度和经度放到数据库中我该怎么做我正在使用 javascript 来获取结果,我将使用 php 作为数据库这是代码我用来获取用户位置

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



function showPosition1(position) {
var locations = [
    ['store1', -2.063150, 2.516503, 4],
    ['store2', -2.064824, 52.518436, 5],
    ['store3', -2.068214, 52.519898, 3],
    ['store4', -2.068558, 52.512769, 2],
    ['store5', -2.070875, 52.510758, 1]
];




lon1 = position.coords.longitude * 0.0174532925;
lat1 = position.coords.latitude * 0.0174532925;
i = 0;

while (i < locations.length) {
    function round(x) {
        return Math.round(x * 10) / 10;
    }
   x.innerHTML += "<br>Distance " + round(calcDist(lon1, lat1, locations[i][1] * 0.0174532925, locations[i][2] * 0.0174532925));
    i++;

}

}

我所有的代码都在工作我只是希望能够将 lon 1 和 lat1 放入我的数据库中,任何想法都会非常感谢提前谢谢

4

1 回答 1

0

您将创建另一个页面来插入数据库。
假设您使用的是 IIS - 您在 C# 或 VB 中编写了一个读取 Request.QueryString["latitude"] 和 Request.QueryString["longitude"] 的页面。
从那里您可以使用标准的 .net 代码来更新 SQL Server。

将数据从浏览器发送到服务器:
在客户端 (javascript) 上,您将使用 jQuery ajax 方法调用该页面以传递您的数据。例如

    $.ajax({
    url: 'url_to_server_page.aspx'
    , data: 'latitude=90&longitude90'})
    , success: function (data) { alert('data'); }
});

如果您不使用 ASP,那么只有您的语言会发生变化,但您如何将数据发送到服务器的基本结构是相同的 - 客户端页面将数据发送到服务器页面。为简单起见,此 ajax 示例使用默认 GET,但由于您正在更新数据库,因此您应该将其作为 POST 执行。

于 2013-09-27T12:24:04.833 回答