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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)