【问题标题】:Store geolocation users in database将地理位置用户存储在数据库中
【发布时间】:2017-11-03 14:11:27
【问题描述】:

几天以来,我一直在尝试找出与我的问题类似的代码有什么问题。到现在为止我还没有成功。

代码如下:

        if (navigator.geolocation) {

            navigator.geolocation.getCurrentPosition(function(position) {
            function successFunction(position) {
                $.ajax({
                        type: 'POST',
                        data: {
                                lat: position.coords.latitude,
                                lng: position.coords.longitude
                                },
                        url: 'template-userslocation.php'
                    });
                }
            });
        }

这里是'template-userslocation.php'中的代码:

    <?php
    /**
     * Template Name: template-userslocation
    */

        echo $lat = $_POST['lat'];
        echo $lng = $_POST['lng'];


    ?>

这是我加载页面时返回的内容:

注意:未定义索引:lat in .../template-userslocation.php on line 8

注意:未定义索引:lng in .../template-userslocation.php on line 9

在此之前的代码可能使其不起作用。 注意:在看到第一个代码中似乎没有错误并得到你们的帮助后,我添加了这个。

这是之前的代码:

    if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position){
                            var pos = {
                                lat: position.coords.latitude,
                                lng: position.coords.longitude
                            };

                            infoWindow.setPosition(pos);
                            infoWindow.setContent('You are here');
                            map.setCenter(pos);
                     },

                function() {
                            handleLocationError(true, infoWindow, map.getCenter());
                    }); 

    } else {
            // Browser doesn't support Geolocation
            handleLocationError(false, infoWindow, map.getCenter());
            };  

我认为var pos = {lat: position.coords.latitude, lng: position.coords.longitude}; 是我遇到的问题之一。

注意:这里是所有代码:

<div class="map">
    <div id="map"></div><!-- #map -->
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAp9iQ2oC3PEvy20_6aDpAPGLT8sFDqAjI&libraries=geometry,places&signed_in=true"></script>     
    <script type="text/javascript">
        google.maps.event.addDomListener( window, 'load', gmaps_results_initialize );

        function gmaps_results_initialize() {
            map = new google.maps.Map( document.getElementById( 'map' ), {
                zoom:           3,
                center:         new google.maps.LatLng( 28.291564, 16.62913 ),
                mapTypeControl: true,

                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.RIGHT_BOTTOM
                },
                zoomControl: true,
                zoomControlOptions: {
                position: google.maps.ControlPosition.LEFT_CENTER
                },

                scaleControl: true,
                streetViewControl: true,
                streetViewControlOptions: {
                    position: google.maps.ControlPosition.LEFT_BOTTOM
                },
                fullscreenControl: true,
                fullscreenControlOptions: {
                position: google.maps.ControlPosition.LEFT_BOTTOM
                }
            });

            var infoWindow = new google.maps.InfoWindow({map: map});

            <?php  if (is_user_logged_in() ) { ?>

                       // Try HTML5 geolocation.

                       if (navigator.geolocation) {                         
                           navigator.geolocation.getCurrentPosition(function (position) {
                               var pos = {
                                  lat: position.coords.latitude,
                                  lng: position.coords.longitude
                               };

                               infoWindow.setPosition(pos);
                               infoWindow.setContent('You are here');
                               map.setCenter(pos);
                           },


                           function() {
                               handleLocationError(true, infoWindow, map.getCenter());
                           }); 


                     } else {
                            // Browser doesn't support Geolocation
                            handleLocationError(false, infoWindow, map.getCenter());
                     };  

                     if (navigator.geolocation) {

                           navigator.geolocation.getCurrentPosition(function (position) {
                               var coords = position.coords;
                               $.ajax({
                                   type: 'POST',
                                   data: {
                                       lat: coords.latitude,
                                       lng: coords.longitude
                                   },
                                   url: 'template-userslocation.php'
                               });

                           });
                    }

        <?php } else; ?>                
        } 

    </script>
</div>

我做错了什么?在我发现类似的其他问题中,他们也根本没有解决。


已解决:

问题在于它是在 Wordpress 中制作的,而 Wordpress 有自己的方式来处理 AJAX。

Here the info

【问题讨论】:

  • var_dump($_POST); 来验证你是否得到了你认为你得到的东西。
  • 在离开浏览器时检查帖子 - 通常您按 F12 键来执行此操作。
  • 我得到数组(0){}。并且在控制台中没有错误。
  • 用 GET 更改了 POST。同样的错误。
  • 在你的successFunction(position) {} AJAX 调用之前添加:console.log(position.coords); 看看你得到了什么?这可能是坐标为空。

标签: php ajax html geolocation


【解决方案1】:

我相信你的网址弄错了,请看:

url: 'template-users-location.php'

用户后面有一个“-”。但你说你的文件名是 template-userslocation.php

【讨论】:

  • 如果是这种情况,POST 不会转到文件,也不会创建通知...
  • 谢谢。但在存档中它是正确的。我在这里写错了。还是非常感谢!问题依然存在。 :(
【解决方案2】:

在您的 ajax 请求中删除数据对象成员周围的单引号:

$.ajax({
      type: 'POST',
      data: {
         lat: position.coords.latitude,
         lng: position.coords.longitude
      },
      url: 'template-userslocation.php'
});

【讨论】:

    【解决方案3】:

    在您的示例中,function successFunction(position) { 永远不会被调用。 然而,经过小的修改,它会这样工作,并在 POST 中传递正确的变量。

    if (navigator.geolocation) {
    
      navigator.geolocation.getCurrentPosition(function(position) {
        var coords = position.coords;
        $.ajax({
          type: 'POST',
          data: {
            lat: coords.latitude,
            lng: coords.longitude
          },
          url: 'template-userslocation.php'
        });
    
      });
    
    }
    

    希望对您有所帮助。

    【讨论】:

    • 非常感谢您的宝贵时间。我试过你告诉我的,但没有成功。我编写代码的方式一定有任何错误。这是正确的吗? echo $coords = $_POST['coords']; 你写给我的条件我把它放在 if 条件后面加上 var pos。一切都在函数function gmaps_results_initialize()里面
    • 我在控制台中得到了这个:Uncaught TypeError: Cannot read property 'ajax' of undefined at (index):459
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多