npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hook-geolocation

v1.1.0

Published

React hook for geolocation access

Downloads

9,648

Readme

react-hook-geolocation :earth_africa:

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