2012-05-22 40 views
-1

我想知道如何在JavaScript中創建某種事件,並在插入新數據時觸發它。如何從數據庫中檢索最後一個結果插入時

我需要這個,所以我可以在谷歌地圖中使用實時跟蹤。

例如,這是我的地理定位頁面上找到的代碼:我使用檢索數據的陣列

function scrollMap(position) 
{ 
// Scrolls the map so that it is centered at (position.coords.latitude,position.coords.longitude). 
} 
// Request repeated updates. 
var watchId = navigator.geolocation.watchPosition(scrollMap); 

function buttonClickHandler() 
{ 
// Cancel the updates when the user clicks a button. 
//I want to put my code in here so for example when I click button live tracking starts. 

    navigator.geolocation.clearWatch(watchId); 
} 

這是我的代碼:

function initialize() { 
    var myLatLng = new google.maps.LatLng(0, 180); 
    var myOptions = { 
    zoom: 3, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 
    var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>]; 
    var flightPath = new google.maps.Polyline({ 
    path: flightPlanCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    }); 

    flightPath.setMap(map); 
} 

哪有我每次將其插入數據庫時​​,讓我的代碼工作並獲取最後一個結果?並在我的問題開始時提供的Google代碼中將其顯示在地圖上。

數據庫是mysql它有ID,經度和緯度。

編輯:

這是我的PHP代碼從數據庫中獲取的所有數據,並把它們在陣列中的谷歌地圖:

$coordinates = array(); 
$result = dbMySql::Exec('SELECT Latitude,Longitude FROM data'); 
while ($row = mysqli_fetch_assoc($result)) 
    $coordinates[] = 'new google.maps.LatLng(' . $row['Latitude'] . ', ' . $row['Longitude'] . ')'; 
+0

對不起,但是什麼數據庫? – Jivings

+0

Mysql我把標記爲mysql –

+0

爲什麼你不把指針放在你的代碼中//這是我想設置一個事件處理程序的地方'//這是我想要觸發事件的地方' –

回答

0

最簡單的方法是輪詢服務器。請求在最近X分鐘內創建的條目並添加新條目。

+0

你能告訴我如何檢查它嗎? –

+0

數據庫沒有更新。只讀 –

+0

@denonth:你的意思是沒有更新數據庫?您的問題是關於在將某些內容插入數據庫時​​收到通知。 –

相關問題