dpevsxjn809817 2015-12-22 05:23
浏览 15
已采纳

使用PHP将当前地理位置加载到数据库中

I'm currently trying to make an application that uploads the users current location into my mysql database. I know how to use google maps in javascript. I know enough to make php work.

What I don't know is how can I take my current location and continually loop it through my database. I was hoping to update the users location every 2 minutes without Post & Get variables. If anybody has any information, any documentation or anything that would be great. I tried looking it up.

I found people saying to use AJAX but I'm not sure how that would loop through my current location without some kind of interaction.

I also saw the other talking about turning it into JSON data.

Any thoughts would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dongpai6567 2015-12-22 07:21
    关注

    I'll answer with simplified concept code for you to get the general idea.

    First you need to add the location polling functionality in your web application with Javascript:

    // We only start location update process if browser supports it
    if ("geolocation" in navigator) 
    {
        request_location();
    } 
    
    // Requesting location from user
    function request_location()
    {
        // Will repeat the process in two minutes
        setTimeout(request_location, 1000*60*2);
    
        // Get location info from browser and pass it to updating function
        navigator.geolocation.getCurrentPosition(update_location);
    }
    
    // Sending location to server via POST request
    function update_location(position)
    {
        // For simplicity of example we'll 
        // send POST AJAX request with jQuery
        $.post("/update-location.php",
        {
            latitude : position.coords.latitude,
            longtitude : position.coords.longitude
        },
        function(){
            // Position sent, may update the map
        });
    }
    

    Then on the server side you must receive the coordinates from the above AJAX request:

    <?php 
    
        $latitude = filter_input(INPUT_POST, 'latitude', FILTER_VALIDATE_FLOAT);
        $longtitude = filter_input(INPUT_POST, 'longtitude', FILTER_VALIDATE_FLOAT);
    
        if($latitude && $longtitude)
        {
            // @TODO: Update database with this data.
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏