JSFiddle - React, Tailwind, and code Playground
by gavinfoley
HTML
<p>Attempting to get location, high accuracy first...</p>
JavaScript
navigator.geolocation.getCurrentPosition(
successCallback,
errorCallback_highAccuracy,
{maximumAge:600000, timeout:5000, enableHighAccuracy: true}
);
function errorCallback_highAccuracy(position) {
if (error.code == error.TIMEOUT)
{
// Attempt to get GPS loc timed out after 5 seconds,
// try low accuracy location
$('body').append("attempting to get low accuracy location");
navigator.geolocation.getCurrentPosition(
successCallback,
errorCallback_lowAccuracy,
{maximumAge:600000, timeout:10000, enableHighAccuracy: true});
return;
}
var msg = "<p>Can't get your location (high accuracy attempt). Error = ";
if (error.code == 1)
msg += "PERMISSION_DENIED";
else if (error.code == 2)
msg += "POSITION_UNAVAILABLE";
msg += ", msg = "+error.message;
$('body').append(msg);
}
function errorCallback_lowAccuracy(position) {
var msg = "<p>Can't get your location (low accuracy attempt). Error = ";
if (error.code == 1)
msg += "PERMISSION_DENIED";
else if (error.code == 2)
msg += "POSITION_UNAVAILABLE";
else if (error.code == 3)
msg += "TIMEOUT";
msg += ", msg = "+error.message;
$('body').append(msg);
}
function successCallback(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$('body').append("<p>Your location is: " + latitude + "," + longitude+" </p><p>Accuracy="+position.coords.accuracy+"m");
}