如何将地理位置变量从客户端传递到服务器端

How to pass a geolocation variable from client to server side

本文关键字:服务器端 客户端 地理位置 变量      更新时间:2023-09-26

有人能给我推荐一种方法吗?在用户允许使用他的地理位置后,立即将客户端Java脚本位置变量(lat,lng)传递到我的服务器端。这样我就可以使用位置变量来查询我的数据库中10公里范围内的商店,并将它们显示在地图上。

我在客户端使用HTML5 Geolocation获取用户位置,在服务器端使用Django+PostGIS数据库进行距离查找。

为了在用户允许使用其地理位置后尽快将用户地理位置从客户端传递到服务器端,

  1. 将用户位置变量(pos)分配给HTML元素(location)
  2. 然后,自动提交一个包含POST方法的HTML表单

以上两个代码都必须在GoogleGeolocationneneneba API中编写,如下摘录所示。

var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'Location found using HTML5.'
  });
  // the below line has been inserted to assign a JS variable to HTML input field called 'geolocation'
document.getElementById('location').value = pos
  map.setCenter(pos);
// the below line has been inserted to autosubmit the form.  
document.getElementById("geolocation").submit(); 

下面是HTML表单

<form id = "geolocation" action="/location" method="POST" >
            {% csrf_token %}
        <input type="text" id = "location" name="location" value="" />
        <input type="submit" />
</form>