dongmi9494 2019-05-14 23:40
浏览 136

使用PHP在MYSQL数据库中存储Geo位置

My website loads the user's current location using GEO location and displays it on a google map when they then click a button, I want it to store the coordinates in the database.

I have already done the GEO location and displaying the coordinates on the map, I just need to take the values from JS and store them in the database inside the if(isset($_POST['newPost'])) statement

javascript

 <script>
        var map, infoWindow;
        function initMap() 
        {
            map = new google.maps.Map(document.getElementById('map'), 
            {
                center: {lat: -34.397, lng: 150.644},
                zoom: 10
            });
            infoWindow = new google.maps.InfoWindow;

            // 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('Location found.');
                    infoWindow.open(map);
                    map.setCenter(pos);
                }, function() 
                {
                    handleLocationError(true, infoWindow, map.getCenter());
                });
            } 
            else 
            {
                // Browser doesn't support Geolocation
                handleLocationError(false, infoWindow, map.getCenter());
            }
        }

        function handleLocationError(browserHasGeolocation, infoWindow, pos) 
        {
            infoWindow.setPosition(pos);
            infoWindow.setContent(browserHasGeolocation ?
                'Error: The Geolocation service failed.' :
                'Error: Your browser doesn\'t support geolocation.');

            infoWindow.open(map);
        }
    </script>

HTML

<form id="newPost" action="index.php" method="Post">
<textarea id="txtMessage" name="body"></textarea>
<hr/>
<br/>
<br/>
<div id="map" style="width:100%;height:400px;"></div>
<br/>
<button type="submit" id="postButton" name="newPost" class="Button">Post! 
</button>
</form>

PHP

if(isset($_POST['newPost']))
{
    DB::query('INSERT INTO heatmap(lon, lan) VALUES (:lon, :lan)', 
        array(':lon'=>$VAL, ':lan'=>$VAL));
}

Any help would be appreciated

  • 写回答

1条回答 默认 最新

  • dougai3418 2019-05-15 01:23
    关注

    Not having a way to test it, I had to make some guesses. I don't see any Markers created, so I assume you're using the map coordinates. Consider the following:

    $(function() {
      var map, infoWindow;
    
      function initMap() {
        map = new google.maps.Map($('#map')[0], {
          center: {
            lat: -34.397,
            lng: 150.644
          },
          zoom: 10
        });
        infoWindow = new google.maps.InfoWindow();
    
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            };
    
            infoWindow.setPosition(pos);
            infoWindow.setContent('Location found.');
            infoWindow.open(map);
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }
      }
    
      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
          'Error: The Geolocation service failed.' :
          'Error: Your browser doesn\'t support geolocation.');
    
        infoWindow.open(map);
      }
    
      $("#newPost").submit(function(e) {
        e.preventDefault();
        var pData = {
          body: $("#txtMessage").val(),
          lng: map.coords.longitude,
          lat: map.coords.latitude
        };
        $.post($(this).attr("action"), pData);
      });
    });
    

    https://jsfiddle.net/Twisty/wzvpnmh6/21/

    This would create POST Data like:

    {
      body: "Hello World",
      lat: -34.397,
      lng: 150.644
    }
    

    You could also Stringify it and send it as a combined string:

    var coords = JSON.Stringify({ lat: c.lat(), lng: c.lng() });
    

    You will then get a string like: {"lat":-34.397,"lng":150.644} that can be added direct to a DB. This is up to you and will be defined by what you need to do with the data. If you need to search for specific Lat or Long, then they will store in their own columns. If you just need the geolocation, then they can be in a single column.

    Hope that helps.

    References:

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记