0

一旦用户允许使用他的地理位置,有人可以建议我一种将客户端Java Script位置变量(lat,lng)传递到我的服务器端的方法。这样我就可以使用 location 变量在我的数据库中查询 10 公里范围内的商店并将它们显示在地图上。

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

4

1 回答 1

2

为了在用户允许使用他的地理位置后立即将用户地理位置从客户端传递到服务器端,通过

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

上述两个代码都必须在 Google Geolocation 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>
于 2013-09-04T14:52:45.967 回答