【问题标题】:Geolocation insert into sql地理定位插入sql
【发布时间】:2015-01-12 16:05:29
【问题描述】:

我想将用户的地理位置解析到我的数据库中。

在我的数据库中有 2 行称为:纬度和经度,我希望将位置存储到这些行中。

这是获取位置的 HTML5 API:

<script>
    var x=document.getElementById("demo");
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.innerHTML="Geolocation is not supported by this browser.";}
        }

    function showPosition(position) {
        x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
    }

    getLocation();

</script>

这里是提交表单的页面

<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fui-cross"></span></button>
                <h4 class="modal-title"><center>Post</center></h4>
            </div>
            <div class="modal-body">
                <form action="parse_post.php" method="post" name="form1" enctype="multipart/form-data">
                    <input name="post_type" type="hidden" value="a" />
                    <div class="form-group">
                        <textarea class="form-control" name="description" rows="6" maxlength="450" placeholder="write something"></textarea>
                    </div>
                    <div class="pull-right">
                    </div>
                    <div class="form-group">          
                        <div id="demo" onLoad=""></div>          
                        <div class="pull-right">
                            <input name="" type="submit" class="btn btn-info" value="Post" onclick="javascript:return validateMyForm();"/> 
                            <input name="uid" type="hidden" value="<?php echo $_SESSION['id']; ?>" />
                            <input name="upass" type="hidden" value="<?php echo $_SESSION['userpass']; ?>" />        
                        </div>
                    </div>

任何帮助将不胜感激 谢谢

【问题讨论】:

  • 附带说明,请确保您处理没有返回位置的情况。 Location API 是可选的,它会提示用户让他们有机会拒绝向您发送此信息。确保您考虑到您没有获得任何价值的情况。
  • 那是什么问题?

标签: javascript php html mysqli


【解决方案1】:

#Demo 不是表单字段,因此当您提交它时,不会将任何内容传递给服务器。试试这个吧

<input id="demo" type="text" />

脚本

<script>
    var x=document.getElementById("demo");
    function getLocation()
    {
        if (navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
        else
        {
            x.value = "Geolocation is not supported by this browser.";
        }
    }

    function showPosition(position)
    {
        x.value ="Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;  
    }
    getLocation();

</script>

更新

我想还有更多。将这些值作为组合字符串发送是没有意义的。改为这样做:

<input id="latitude" name="latitude" type="text" />
<input id="longitude" name="longitude" type="text" />

脚本

<script>
    function getLocation()
    {
        if (navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
    }

    function showPosition(position)
    {
        document.getElementById("latitude").value = position.coords.latitude; 
        document.getElementById("longitude").value = position.coords.longitude; 
    }
    getLocation();

</script>

【讨论】:

  • 感谢帮助使用sql将经纬度解析为我的数据库中的经纬度行。什么是正确的方式。在 parse_post.php 中:$longitude = $_POST['longitude']; $latitude = $_POST['latitude'];$sql = mysqli_query($conn,"INSERT INTO forum_posts (latitude, longitude) VALUES('$post_author','$member_id','$latitude','$longitude') ") 或死 (mysql_error());
  • 一旦我们从用户那里获得位置数据,最好将其存储在整个会话中,以便我们可以在其他页面上使用它。有没有简单的解决方案?
  • 使用 PHP Session 变量。网上有很多例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 2015-01-11
  • 2017-08-17
  • 1970-01-01
相关资源
最近更新 更多