I am using Geolocation API to detect the location of the user. I want now to get the location of the user on the smartphone. I have read the thread on I set geolocation permission in the manifest, but every page still asks to share location. But it is 8 years old. Is it possible to get the location of the user on smartphone? Should I write the Manifest for every single browser?
This is my code for this:
<script>
const geoinfo = document.getElementById("geoinfo"); // not necessary thanks to IE quirk but good practice
navigator.geolocation.getCurrentPosition = function(success, error, options) { ... };
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
const { latitude, longitude, altitude } = position.coords;
//geoinfo.innerHTML = `Breitengrad: ${latitude}, Längengrad: ${longitude}`;
if (altitude) geoinfo.innerHTML += `, Höhe über Meeresspiegel: ${altitude}`;
// send to server
sendData(latitude, longitude);
});
} else {
geoinfo.innerHTML = 'Dieser Browser unterstützt die Abfrage der Geolocation nicht.';
}
async function sendData(lat, long) {
const response = await fetch(`?lat=${lat}&long=${long}`); // send data to this script, regardless of filename
if (response.ok) {
const text = await response.text();
if (text == "received") alert("Geodaten erfolgreich zum Server gesendet.");
}
}
</script>
It is only working on Desktop PC´s....
This code which I found from the doc does not work?
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
};
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
};
navigator.geolocation.getCurrentPosition(success, error, options);