【发布时间】:2021-07-05 02:02:42
【问题描述】:
我正在寻找一种使用 HTML5(可能还有 JS)将访问者/用户经纬度数据保存到数据库的方法。我不希望在外面使用包,因为它们似乎有点过时,并且考虑到它们自己对其他 API 的依赖,将来可能会破坏我的代码。
我知道有一种使用 AJAX 的方法,但我显然不了解和理解它以实现它。
我对学识渊博的领主的要求是 - 1. 获取 Loc 数据 2. 以 dict 或 json 或字符串格式将其发送到 Python,从中可以进一步处理和保存。
您为什么会问 - 好问题。我会用它来显示主页上的天气和“登录”页面上的本地推特趋势。
任何帮助将不胜感激。
干杯!
我的 JS 代码如下:
// Set up global variable
var result;
function showPosition() {
// Store the element where the page displays the result
result = document.getElementById("result");
// If geolocation is available, try to get the visitor's position
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
result.innerHTML = "Getting the position information...";
} else {
alert("Sorry, your browser does not support HTML5 geolocation.");
}
};
// Define callback function for successful attempt
function successCallback(position) {
result.innerHTML = [position.coords.latitude, position.coords.longitude];
}
// Define callback function for failed attempt
function errorCallback(error) {
if (error.code == 1) {
result.innerHTML = "You've decided not to share your position, but it's OK. We won't ask you again.";
} else if (error.code == 2) {
result.innerHTML = "The network is down or the positioning service can't be reached.";
} else if (error.code == 3) {
result.innerHTML = "The attempt timed out before it could get the location data.";
} else {
result.innerHTML = "Geolocation failed due to unknown error.";
}
}
window.onload = showPosition;
【问题讨论】:
-
这是否满足您的需求? MDN | Geolocation API
-
在您的前端,您可以使用 JS 调用 api 并使用位置更新用户配置文件。 w3schools.com/html/html5_geolocation.asp
-
@shadow-lad:感谢您的链接,但我已经有了纬度和经度...我是新手,因此不知道如何附加屏幕截图。如前所述。我只是在寻找一种将 JS 值传输到 Python 以进行分析和 Twitter 趋势的解决方案。
-
@LinhNguyen 在前端一切工作正常......我有那里需要的东西......我需要将它发送回 Django / Python 进行分析。非常感谢。
标签: python html django location geo