在 Flask Web 应用程序中使用 JS 地理定位 (Python 3.6.6)

Using JS Geolocation in Flask Web App (Python 3.6.6)

我正在使用 Heroku 在 Flask 中开发一个网络应用程序,并试图将纬度和经度数据合并到该应用程序中(我想从我的 login.html 中获取用户当前的 lat/lon文件并将其与我的 application.py 文件中预先指定的 lat/lon 进行比较)。我编写了代码的 JS 组件,但我不确定如何处理它 - 如何从 JS 获取地理定位数据并在 Flask 中使用它?另外,我怎样才能看到用户的当前 lat/lon?

非常非常感谢您对此提供的任何帮助。 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 %}

使用 ajax 怎么样? 您可能需要在 JS 中使用 ajax 函数。 我为你搜索了所有关于SO的相关文章,我认为你可以申请。我验证它有效。希望对您有所帮助:)