【发布时间】:2018-09-25 07:39:11
【问题描述】:
我正在使用 Heroku 在 Flask 中开发一个网络应用程序,并尝试将纬度和经度数据合并到应用程序中(我想从我的 login.html 文件中获取用户当前的纬度/经度并将其与我的 application.py 文件中预先指定的纬度/经度)。我编写了代码的 JS 组件,但我不确定如何处理它 - 如何从 JS 获取地理位置数据并在 Flask 中使用它?另外,我怎样才能看到用户当前的纬度/经度?
对此的任何帮助都非常非常感谢。 JS代码:
{% extends "layout.html" %}
{% block title %}
Log In
{% endblock %}
{% block main %}
<form action="/login" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text"/>
</div>
<div class="form-group">
<input class="form-control" name="password" placeholder="Password" type="password"/>
</div>
<button class="btn btn-primary" type="submit">Log In</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
//run this code when the page loads
jQuery(document).ready(function(){
//call the getGeolocation function below
getGeolocation();
});
//determine if the user's browser has location services enabled. If not, show a message
function getGeolocation() {
if(navigator.geolocation){
//if location services are turned on, continue and call the getUserCoordinates function below
navigator.geolocation.getCurrentPosition(getUserCoodinates);
}else{
alert('You must enable your device\'s location services in order to run this application.');
}
}
//function is passed a position object which contains the lat and long value
function getUserCoodinates(position){
//set the application's text inputs LAT and LONG = to the user's lat and long position
jQuery("#LAT").val(position.coords.latitude);
jQuery("#LONG").val(position.coords.longitude);
}
// ]]></script>
{% endblock %}
【问题讨论】:
标签: javascript python python-3.x flask geolocation