I created a small POC of showing a marker at the actual geolocation in Google Maps Javascript API. It works in general, but I realise that the interval of geolocation updates is very high. I mean, it takes a lot of trys until a new geolocation updated.
Then I realised that I don't have this problem in Google Maps Android App. Here, the geolocation is updated nearly simultaniously. And: After it is updated in Google Maps App, also the Javascript API in the browser receives a new value. You can see it here in a video of my Phone: https://gfycat.com/calmspiffyamericanbittern
So, it seams to me that the Android App has a possibility to force geolocation updates in really short intervals, while the Javascript API from the Browser doesn't.
You can try the POC yourself on https://michaelfirges.de/maps/
This is the code:
(() => {
const message = document.querySelector('#message');
// check if the Geolocation API is supported
if (!navigator.geolocation) {
message.textContent = `Your browser doesn't support Geolocation`;
message.classList.add('error');
return;
}
// handle click event
const btn = document.querySelector('#show');
btn.addEventListener('click', function () {
// get the current position
navigator.geolocation.getCurrentPosition(onSuccess, onError, {
maximumAge: 0, timeout: 5000, enableHighAccuracy:true} );
});
// handle success case
function onSuccess(position) {
const {
latitude,
longitude
} = position.coords;
var markerpos = new google.maps.LatLng(latitude, longitude);
const marker = new google.maps.Marker({
position: markerpos,
map: map,
draggable: true,
});
message.classList.add('success');
message.textContent = `Your location: (${latitude},${longitude})`;
}
// handle error case
function onError() {
message.classList.add('error');
alert('Failed to get your location!');
}
})();
Big thanks for any input!