Coder Social home page Coder Social logo

react-hook-geolocation's Introduction

react-hook-geolocation 🌍

A React hook to access data from the Geolocation API.

Installation

Using npm:

npm install --save react-hook-geolocation

Using yarn:

yarn add react-hook-geolocation

Usage

import useGeolocation from "react-hook-geolocation";

const ComponentWithGeolocation = () => {
  const geolocation = useGeolocation();

  return !geolocation.error ? (
    <ul>
      <li>Latitude: {geolocation.latitude}</li>
      <li>Longitude: {geolocation.longitude}</li>
      <li>Location accuracy: {geolocation.accuracy}</li>
      <li>Altitude: {geolocation.altitude}</li>
      <li>Altitude accuracy: {geolocation.altitudeAccuracy}</li>
      <li>Heading: {geolocation.heading}</li>
      <li>Speed: {geolocation.speed}</li>
      <li>Timestamp: {geolocation.timestamp}</li>
    </ul>
  ) : (
    <p>No geolocation, sorry.</p>
  );
};

Using PositionOptions

There is a way to use PositionOptions to fine tune response coming from watchPosition of the Geolocation API.

If you want to use this feature, simply provide useGeolocation with a PositionOptions object:

const geolocation = useGeolocation({
  enableHighAccuracy: true,
  maximumAge: 15000,
  timeout: 12000,
});

Using a callback function

You can supply a second parameter to useGeolocation which will be called every time the data from the Geolocation API is updated. This callback function is then called with the geolocation object with all its properties.

If you don't use PositionOptions, I recommend that you supply {} as your first argument.

const onGeolocationUpdate = (geolocation) => {
  console.log("Here’s some new data from the Geolocation API: ", geolocation);
};

const geolocation = useGeolocation({}, onGeolocationUpdate);

Waiting for the user to opt in

The best practice is to only try to geolocate the user on the user’s request, or at least when it is indicated to the user why the browser is asking for their location.

If you would like to bail out of this hook immediately asking for geolocation access from the user when the component mounts, you can pass a third isEnabled parameter to the hook with the value false, and set it to true later once the reason for asking for geolocation is indicated to the user.

const geolocation = useGeolocation({}, () => {}, false);

Setting this parameter from true to false will not cut access to already retrieved geolocation data, it will however stop watching for changes in geolocation data until the parameter is set back to true again.

Notes

Access to data from the Geolocation API needs user permission.

If permission to access geolocation was previously granted by the user, geolocation data will be available. If permission to access was not granted previously, the user will be prompted to give permission when the component mounts.

If permission was previously denied by the user, if the user agent does not support the Geolocation API, or if some other error occurs, the object returned from the hook will still contain all members, and values will be null. If you are lucky, geolocation.error.message and geolocation.error.code might contain something useful to help you determine what went wrong.

Caveats

Geolocation API is available only in secure contexts (a.k.a. only using HTTPS).

Getting geolocation data can take time, especially with high accuracy enabled – getting a GPS fix can take up to a minute.

Contributions

Contributions are welcome. File bug reports, create pull requests, feel free to reach out at [email protected].

License

MIT

react-hook-geolocation's People

Contributors

bence-toth avatar dependabot[bot] avatar emad-emami avatar nathanmmiller avatar rasmuslp avatar rootenginear avatar tremby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-hook-geolocation's Issues

React context

Hi Bence I use your geolocation hook and you did an amazing job!
Can you show an example how to store the geolocation data in react context api?

Is geolocation.heading the bearing?

Hi again, thanks a lot for your help in advance.

Just wondering if the bearing (direction in wich the movil is moving) is represented by the geolocation.heading.
I am not sure how to understand this data value.

Thank you

Add an option to disable geolocation

Best practice is to only try to geolocate the user on the user's request, or at least when it's clear to the user why the browser is asking for their location.

React doesn't let us conditionally use hooks, short of conditionally rendering the component itself which uses the hook. If I make a conditionally-rendered nested component and use the hook from there, I'd need to pass the location data up the tree back to the parent component, which is not the React way.

I'd like to see an enable (default true) or disable (default false) option, so I can still use the hook but not have it actually do anything until I flip that option in response to the user saying "geolocate me".

Using the library causes an error to occur

image

I use the library in a project in combination with:

  • Typescript
  • NextJs

I import the module like this:

import useGeolocation from 'react-hook-geolocation'

Am I doing something wrong?

Add Typescript Typings

This will be useful for folks who code in TS.

I'll contribute a PR for this very shortly.

How to convert speed to Km/h

Hi all!

I am not sure what type of value is the speed. how could I convert it to km/h?

Thanks & I hope you guys can give me a heads up.

Add types

Would be great to have types with this :)

Operability improvements

  • Update dev dependencies
  • Add ESLint (+rule sets) and prettier to dev dependencies
  • Add npm scripts to run prettier and eslint
  • Create GH Actions workflow to run build, linter, and prettier

Setting to control GPS recursivity

Hi there again 👯

I am using the hook and is updating data every two seconds or so.
Just wondering if there is a setting to control this behavoir and set this time to ms if possible?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.