Skip to content

cezarsmpio/pozition.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pozition.js

Geolocation Position. Plus Promise.

Yes. The Geolocation API is so simple, and you need to answer yourself: Do I really need that?

My motivations to create that lib are: learn TypeScript and develop a simple Geolocation "adapter" using promises instead of callbacks.

Demo

Install

npm

npm install pozition.js --save

yarn

yarn add pozition.js

cdn

<script src="https://cdn.rawgit.com/cezarlz/pozition.js/ac4d0d20/dist/pozition.min.js"></script>

Example

pozition.current()
  .then(position => {
    console.log(position.coords.latitude);
    console.log(position.coords.longitude);
    console.log(position.coords.speed);
  })
  .catch(err => {
    console.log(err.code);
    console.log(err.message);
  });

You can pass options as well:

const options = {
  enableHighAccuracy: true,
  timeout: 30
};

pozition.current(options)
  .then(position => {
    console.log(position.coords.latitude);
    console.log(position.coords.longitude);
    console.log(position.coords.speed);
  })
  .catch(err => {
    console.log(err.code);
    console.log(err.message);
  });

API

pozition.current(options: PositionOptions)

Gets the current position returning a object with the user's coordinates.

MDN says: Determines the device's current location and gives back a Position object with the data.

Result:

interface Position {
    readonly coords: Coordinates;
    readonly timestamp: number;
}

interface Coordinates {
    readonly accuracy: number;
    readonly altitude: number | null;
    readonly altitudeAccuracy: number | null;
    readonly heading: number | null;
    readonly latitude: number;
    readonly longitude: number;
    readonly speed: number | null;
}

pozition.watch(options: PositionOptions)

Gets the current position and still watching the position returning a object with the user's coordinates every time user moves.

MDN says: Returns a long value representing the newly established callback function to be invoked whenever the device location changes.

Result:

interface Position {
    readonly coords: Coordinates;
    readonly timestamp: number;
}

interface Coordinates {
    readonly accuracy: number;
    readonly altitude: number | null;
    readonly altitudeAccuracy: number | null;
    readonly heading: number | null;
    readonly latitude: number;
    readonly longitude: number;
    readonly speed: number | null;
}

pozition.clear(id)

Removes the particular handler previously installed using watch().

PositionOptions interface

You can pass as options to the methods the following parameters:

interface PositionOptions {
    enableHighAccuracy?: boolean;
    maximumAge?: number;
    timeout?: number;
}

Async & Await

Yes, we can use it, take a look:

const getUserPosition = async function () {
  const { coords } = await pozition.current();

  console.log(coords);

  return coords;
}

JS is awesome!


Another lib made with ❤️

About

Geolocation Position. Plus Promise.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published