我試圖建立一個跟蹤器,只需要一個瀏覽器窗口打開,在移動設備上。所有的代碼似乎工作,比其他navigator.gelocation.getCurrentPosition。正如你所看到的,我使用定時器週期我的代碼,每隔幾秒鐘。我期待的時候輸出的實時位置信息中的代碼,而是似乎要利用到最後已知的位置 - 或者,如果沒有一個,它會找到的位置,然後繼續使用相同的一個。navigator.geolocation.getCurrentPosition不會與新位置重新在Android
我不確定這是Android或我的代碼的問題,但它似乎在iPhone上運行良好 - 但它不是我測試它,所以我不能確定。
在此先感謝!
var lati = " ";
var longi = " ";
\t
\t function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
\t \t \t \t \t zoom: 15
});
\t var timerID = setInterval(function() { \t
\t
\t
\t
// Try HTML5 geolocation.
if (navigator.geolocation) {
\t \t var infoWindow = new google.maps.InfoWindow({map: map}); \t
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
\t \t \t lati = (position.coords.latitude);
\t \t \t longi = (position.coords.longitude);
\t \t \t console.log(lati, "Lol");
infoWindow.setPosition(pos);
infoWindow.setContent('lati');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
\t \t
});
} 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.');
}
\t \t
\t //in a loop (setInterval) get coords and apply them to database
\t
\t
\t {
\t $.ajax({ url: 'php_test.php',
data: {'Lati': lati, 'Longi': longi},
type: 'post',
dataType:'json',
success: function(output) {
alert(output);
}
});
\t
\t }
\t
\t
\t }, 2 * 1000);
\t
\t }