• Stars
    star
    298
  • Rank 139,663 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŒ React hook usePosition() for fetching and following a browser geolocation

React hook for following a browser geolocation

npm version codecov

React hook usePosition() allows you to fetch a client's browser geolocation and/or subscribe to all further geolocation changes.

โ–ถ๏ธŽ Storybook demo of usePosition() hook.

Installation

Using yarn:

yarn add use-position

Using npm:

npm i use-position --save

Usage

Import the hook:

import { usePosition } from 'use-position';

Fetching client location

const {
  latitude,
  longitude,
  speed,
  timestamp,
  accuracy,
  heading,
  error,
} = usePosition();

Following client location

In this case if browser detects geolocation change the latitude, longitude and timestamp values will be updated.

const watch = true;
const {
  latitude,
  longitude,
  speed,
  timestamp,
  accuracy,
  heading,
  error,
} = usePosition(watch);

Following client location with the highest accuracy

The second parameter of usePosition() hook is position options.

const watch = true;
const {
  latitude,
  longitude,
  speed,
  timestamp,
  accuracy,
  heading,
  error,
} = usePosition(watch, { enableHighAccuracy: true });

Full example

import React from 'react';
import { usePosition } from 'use-position';

export const Demo = () => {
  const watch = true;
  const {
    latitude,
    longitude,
    speed,
    timestamp,
    accuracy,
    heading,
    error,
  } = usePosition(watch);

  return (
    <code>
      latitude: {latitude}<br/>
      longitude: {longitude}<br/>
      speed: {speed}<br/>
      timestamp: {timestamp}<br/>
      accuracy: {accuracy && `${accuracy} meters`}<br/>
      heading: {heading && `${heading} degrees`}<br/>
      error: {error}
    </code>
  );
};

Specification

usePosition() input

  • watch: boolean - set it to true to follow the location.
  • settings: object - position options
    • settings.enableHighAccuracy - indicates the application would like to receive the most accurate results (default false),
    • settings.timeout - maximum length of time (in milliseconds) the device is allowed to take in order to return a position (default Infinity),
    • settings.maximumAge - the maximum age in milliseconds of a possible cached position that is acceptable to return (default 0).

usePosition() output

  • latitude: number - latitude (i.e. 52.3172414),
  • longitude: number - longitude (i.e. 4.8717809),
  • speed: number | null - velocity of the device in meters per second (i.e. 2.5),
  • timestamp: number - timestamp when location was detected (i.e. 1561815013194),
  • accuracy: number - location accuracy in meters (i.e. 24),
  • heading: number | null - direction in which the device is traveling, in degrees (0 degrees - north, 90 degrees - east, 270 degrees - west, and so on),
  • error: string - error message or null (i.e. User denied Geolocation)

More Repositories

1

javascript-algorithms

๐Ÿ“ Algorithms and data structures implemented in JavaScript with explanations and links to further readings
JavaScript
173,558
star
2

homemade-machine-learning

๐Ÿค– Python examples of popular machine learning algorithms with interactive Jupyter demos and math being explained
Jupyter Notebook
21,617
star
3

learn-python

๐Ÿ“š Playground and cheatsheet for learning Python. Collection of Python scripts that are split by topics and contain code examples with explanations.
Python
14,972
star
4

state-of-the-art-shitcode

๐Ÿ’ฉState-of-the-art shitcode principles your project should follow to call it a proper shitcode
4,789
star
5

nano-neuron

๐Ÿค– NanoNeuron is 7 simple JavaScript functions that will give you a feeling of how machines can actually "learn"
JavaScript
2,192
star
6

promote-your-next-startup

๐Ÿš€ Free resources you may use to promote your next startup
2,000
star
7

js-image-carver

๐ŸŒ… Content-aware image resizer and object remover based on Seam Carving algorithm
TypeScript
1,504
star
8

machine-learning-experiments

๐Ÿค– Interactive Machine Learning experiments: ๐Ÿ‹๏ธmodels training + ๐ŸŽจmodels demo
Jupyter Notebook
1,461
star
9

machine-learning-octave

๐Ÿค– MatLab/Octave examples of popular machine learning algorithms with code examples and mathematics being explained
MATLAB
796
star
10

self-parking-car-evolution

๐Ÿงฌ Training the car to do self-parking using a genetic algorithm
TypeScript
688
star
11

covid-19

๐Ÿ“ˆ Coronavirus (COVID-19) dashboard to show the dynamics of ะกoronavirus distribution per country
JavaScript
265
star
12

nodejs-master-class

๐Ÿ›  This repository contains the homework assignment for Node.js Master Class that is focused on building a RESTful API, web app GUI, and a CLI in plain Node JS with no NPM or 3rd-party libraries
JavaScript
235
star
13

angular-library-seed

๐ŸŒพ Seed project for Angular libraries that are AOT/JIT compatible and that use external SCSS-styles and HTML-templates
TypeScript
203
star
14

okso-app

โœ๐Ÿป The drawing app to express, grasp, and organize your thoughts and ideas. Draw to explain. Draw to grasp.
198
star
15

links-detector

๐Ÿ“– ๐Ÿ‘†๐Ÿป Links Detector makes printed links clickable via your smartphone camera. No need to type a link in, just scan and click on it.
TypeScript
184
star
16

trekhleb.github.io

๐Ÿงฌ My personal website with a list of my projects that help people learn and blog posts about life, web-development, and machine-learning.
TypeScript
176
star
17

hello-docker

๐Ÿณ Example Docker project that is used as illustration for automated continuous delivery flow with DockerCloud and DigitalOcean
Python
48
star
18

micrograd-ts

๐Ÿค– A TypeScript version of karpathy/micrograd โ€” a tiny scalar-valued autograd engine and a neural net on top of it
TypeScript
42
star
19

giphygram

๐Ÿ”Ž Experimental React application for searching GIF images on GIPHY
JavaScript
24
star
20

vscode-search-tree

๐Ÿ”Ž (Draft!) VSCode extension to show the search results in a tree view
TypeScript
21
star
21

trekhleb

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป My GitHub profile intro
15
star