I'm trying to get the location of a device at the time of loading a web page and passing that through to two variables in PHP (lat, long) so it can be manipulated further down the script. Currently, I have the data entered manually by tasker through the URL, but I'd like to have it available in a normal web page too without having to put the data into the URL by hand each time.
I was trying to use the getCurrentPosition() JavaScript to do this, but I can't find a way to get it back into the PHP. I have tried to write two separate files but all I seem to get back from the file is the JS script because it has not yet processed the file. So not the actual data itself.
PHP File:
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
ob_start();
$location = file_get_contents('https://myfakeurl.com/getlocation.php', false, stream_context_create($arrContextOptions));
$GPSCoord = explode(",", $location);
$coord['lat'] = trim($GPSCoord[0]);
$coord['long'] = trim($GPSCoord[1]);
echo "Longitude: " . $coord['long'] . "<br>";
echo "Lattitude: " . $coord['lat'] . "<br>";
?>
and JS file:
<p id="coord"></p>
<script>
var x = document.getElementById("coord");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = position.coords.latitude + "," +
position.coords.longitude;
}
getLocation()
</script>
The JS needs to run on the devices too which is part of the problem otherwise you don't get the correct GPS coords
How can I make this work? I feel like this is an Alice in Wonderland rabbit hole...