2015-12-22 75 views
1

我正在嘗試製作一個應用程序,將用戶當前位置上傳到我的mysql數據庫。我知道如何在JavaScript中使用Google地圖。我知道足夠讓PHP工作。使用PHP將當前地理位置加載到數據庫中

我不知道的是我該如何取得我目前的位置,並不斷循環訪問我的數據庫。我希望每2分鐘更新一次用戶位置,而不需要Post &獲取變量。如果任何人有任何信息,任何文件或任何將是偉大的。我試圖查找它。

我發現有人說使用AJAX,但我不知道如何在沒有某種交互的情況下循環瀏覽當前位置。

我也看到了另外一個關於把它變成JSON數據的討論。

任何想法將不勝感激!

+0

你需要更加細緻。你是如何使你的PHP工作?和這個PHP做什麼? – user4804138

+0

對不起。我想將我的位置更新到我的數據庫。同時我希望其他用戶更新他們的位置,然後我希望它每隔幾分鐘刷新一次每個人的位置。 目的是讓每個人都可以看到其他人在地圖上的位置。 – Matodobra24

回答

1

我會用簡化的概念代碼給你答案,以獲得大致的想法。

首先,你需要在你的web應用程序的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 
    }); 
} 

然後在服務器端,你必須從上述AJAX請求接收座標:

<?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. 
    } 
+0

非常感謝您的回答。我確實有一個額外的問題。我正確地設置了一切。我已檢查並驗證它正在運行代碼。一旦它到達PHP部分,我無法得到它來更新數據庫。當我將它設置爲$緯度時,它將我的數據庫值重置爲0.00000。對不起,但我並沒有要求你爲我寫代碼,但我似乎無法找到這些信息。 – Matodobra24

+0

無視。我明白了,這很簡單。謝謝你的幫助! – Matodobra24

相關問題