我已经使用下面的代码,将Google地理位置API中的JavaScript位置变量(pos)分配给HTML表单中的一个输入元素(id="location")。
document.getElementById('location').value = pos
以下是我的HTML表单。
<form id = "geolocation" action="/location" method="POST" >
{% csrf_token %}
<input type="text" id = "location" name="location" value="" />
<input type="submit" />
</form>
然后在我的谷歌地理位置API中,我通过添加以下代码行来自动提交表单。
document.getElementById("geolocation").submit();
最后,在Django的Views.py文件中,我使用'POST'方法在使用变量的函数内从客户端获取位置。
user_location = request.POST.get('location')
这里是谷歌地理定位API的链接。
我插入了一段代码摘录,以便您知道我在谷歌地理定位API中使用上述JavaScript代码的具体位置。
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
document.getElementById('location').value = pos
map.setCenter(pos);
document.getElementById("geolocation").submit();
$.post("/location", {position : pos});
替代。在服务器端通过request.POST['position']
访问它(我想)。 - Joe(document).ready(function()
在用户点击允许(发送当前位置)之前就被执行了? - kurrodu