Skip to main content

Geolocation

Utilities to work with geolocation links and coordinates. Available since version 1.9.2.

Get current position

This is a wrapper around navigator.getCurrentPosition() with some modern adaptations.

The request is promisified and the Error is completed with the name parameter.

import { Geolocation } from 'eyeson';

try {
const { latitude, longitude } = await Geolocation.getCurrentPosition();
} catch (error) {
if (error.name === 'NotAllowedError') {
console.log('The page didn\'t have the necessary permissions, for example because it is blocked.');
}
else if (error.name === 'PositionUnavailableError') {
console.log('Geolocation failed because at least one internal source of position returned an internal error.');
}
else if (error.name === 'TimeoutError') {
console.log('The time allowed to acquire the geolocation was reached.');
}
}
note

The result also contains accuracy, altitude, heading, and speed. See: https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates

Good to know: The Error is still a GeolocationPositionError, so you can also match the code parameter.

if (error instanceof GeolocationPositionError) {
switch (error.code) {
case GeolocationPositionError.PERMISSION_DENIED:
break;
case GeolocationPositionError.POSITION_UNAVAILABLE:
break;
case GeolocationPositionError.TIMEOUT:
break;
}
}

Check support

The geolocation support check can be found in FeatureDetector. It can be used together with Geolocation.getCurrentPosition().

import { FeatureDetector } from 'eyeson';

const isSupported = FeatureDetector.canGeolocation();

Permission check

This can be called any time before Geolocation.getCurrentPosition(). Show a hint to the user if permission is denied. No try-catch needed, in case of an error "prompt" is returned.

import { Geolocation } from 'eyeson';

const state = await Geolocation.getPermissionStatus();
switch (state) {
case 'prompt':
break;
case 'granted':
break;
case 'denied':
break;
}

Helper functions to generate or parse links with location information with common online maps services.

Given latitude and longitude values, you can generate links to a number of common online maps services like Google Maps, etc.

import { Geolocation } from 'eyeson';

const latitude = 48.2154648; // double or string
const longitude = 16.397599; // double or string
const {
what3words,
googleMaps,
openStreetMap,
bingMaps,
wegoHere
} = Geolocation.generateLinkFromCoordinates(latitude, longitude);

console.log(googleMaps); // https://www.google.com/maps/search/?api=1&query=48.2154648,16.397599

Extract latitude and longitude values from links of common online maps services.

Currently supported:

  • Google Maps
  • OpenStreetMap
  • Bing Maps
  • HERE WeGo
import { Geolocation } from 'eyeson';

if (Geolocation.linkContainsCoordinates(link)) {
const { latitude, longitude } = Geolocation.parseCoordinatesFromLink(link);
}

// OR

const parsed = Geolocation.parseCoordinatesFromLink(link);
if (parsed !== null) {
const { latitude, longitude } = parsed;
}