JavaScript
Geolocation
Get user geolocation information.
By EZ4Code Team
geolocationgeolocation
Code
function getPosition() {
return new Promise((resolve, reject) => {
if (!navigator.geolocation) return reject(new Error("Geolocation not supported"));
navigator.geolocation.getCurrentPosition(
pos => resolve({
lat: pos.coords.latitude,
lng: pos.coords.longitude,
accuracy: pos.coords.accuracy
}),
err => reject(err),
{ enableHighAccuracy: true, timeout: 10000 }
);
});
}Explanation
Wraps the Geolocation API in Promise form.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Array Deduplication
Deduplicate an array using Set.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.