以下代码,我用于在 jsp/javascript/html 中获取地理位置。
<script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
alert(position.coords.latitude + ", " + position.coords.longitude);
});
}
</script>
现在,我需要将这些值存储在数据库中,该查询位于 jsp 标记中。 我们如何用变量存储这些纬度、经度值,然后传递给查询。
最佳答案
选项 1:-
如果您要在 jsp 中提交任何表单,请输入如下所示的隐藏值
<input type="hidden" id="geoLocation" >
然后在页面开始加载时(即在 windows.onLoad
中)或在加载洞页面(即在 jQuery 中)$document.ready{}
将值设置为
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
document.getElementById("geoLocation").value=position.coords.latitude + ", " + position.coords.longitude;
}
);
}
然后用表单提交...
选项 2:-
尝试使用AJAX DWR该组件允许您使用单个 DAO 类(即简单的 POJO 类)将数据添加到数据库
其官方网站上列出了所有操作步骤:- http://directwebremoting.org/dwr/introduction/getting-started.html
如果您使用 MVC,您将需要在您的 jsp 中包含 2 个 javascript 文件,并授予 DWR 访问您的 DAO 方法的权限,但在这种情况下,我更喜欢为 DWR 创建单独的 DAO,您就完成了...
关于javascript - 如何在jsp中将地理位置值传递给变量和sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897743/