Thanks but I just can’t get things to work…aaaaarg (so frustrated).
The app that runs the browser location checking is called BrowserCheck. (the code is further down).
I then wrote the receiving blank app (called LocationUpdate the code is also further down) that receives the data (It works when you call it from your browser).
The primary job of the apps is to get the browsers lat/lng and consistently call LocationUpdate (thus updating the users lat/lng. I know my loop is working as I can see it looping through and showing the results using x.innerHTML.
To send the lat and lng I have tried with no luck:
var xhttp = new XMLHttpRequest();
xhttp.open(“POST”, “https://www.xxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat, true);
xhttp.send();
and your ajax.
The only thing that seems to work is, which is crazy (why open another window)
let url = “https://www.xxxxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat ;
window.open(url,’_blank’);
BrowserCheck App:
$lat=’’;
$lng=’’;
$deviceID=[deviceID];
The script part starts here
var x = document.getElementById(“location”);
var lng = “”;
var lat = “”;
var deviceID = “”;
var $timezone = “”;
function timedRefresh(timeoutPeriod) {
setTimeout(“location.reload(true);”,timeoutPeriod);
}
window.onload = timedRefresh(10000);
setInterval(function() {
getLocation();
} , 10);
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = “Geolocation is not supported by this browser.”;
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var deviceID = <?php echo json_encode($_SESSION['deviceID']); ?>;
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// var xhttp = new XMLHttpRequest();
// xhttp.open(“POST”, “https://www.xxxxxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat, true);
// xhttp.send();
// let url = “https://www.xxxxx/LocationUpdate/LocationUpdate.php?deviceID=” + deviceID + “&lng=” + lng + “&lat=” + lat;
// window.open(url,’_blank’);
x.innerHTML = "Latitude: " + lat + " Longitude: " + lng + " DeviceID: "+ deviceID;
}
function errorHandler(err) {
if(err.code == 1) {
alert(“Error: Please ENABLE GPS on your device!”);
}
else if( err.code == 2) {
// alert(“Error: Position is unavailable!”);
}
}
LocationUpdate app (code below):
$lat=[lat];
$lng=[lng];
$deviceID=[deviceID];
if($lat <> “0”){
$update=date(“Y-m-d h:i:s”);
$update_table = ‘IsawDevices’;
$update_where = “deviceID = ‘$deviceID’”;
$update_fields = array(
“lat = ‘$lat’”,
“lng = ‘$lng’”,
);
$update_sql = ‘UPDATE ’ . $update_table
. ’ SET ’ . implode(’, ', $update_fields)
. ’ WHERE ’ . $update_where;
sc_exec_sql($update_sql);
sc_commit_trans();
// echo “Updated: lat=”.$lat." lng=".$lng;
}
else
{
// echo “No Update”;
}
//sc_exit();