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-geolocated

v4.2.0

Published

React hook for using Geolocation API

Downloads

51,033

Readme

Node.js CI npm version semantic-release

react-geolocated - React hook for using Geolocation API

Demo

Basic demo can be found at the demo page.

HOC version

This package used to be a HOC, not a hook. If you want to use the HOC version, please stick with versions < 4.

Basic Usage

Install using npm:

npm install react-geolocated --save

Then use in your application like this:

import React from "react";
import { useGeolocated } from "react-geolocated";

const Demo = () => {
    const { coords, isGeolocationAvailable, isGeolocationEnabled } =
        useGeolocated({
            positionOptions: {
                enableHighAccuracy: false,
            },
            userDecisionTimeout: 5000,
        });

    return !isGeolocationAvailable ? (
        <div>Your browser does not support Geolocation</div>
    ) : !isGeolocationEnabled ? (
        <div>Geolocation is not enabled</div>
    ) : coords ? (
        <table>
            <tbody>
                <tr>
                    <td>latitude</td>
                    <td>{coords.latitude}</td>
                </tr>
                <tr>
                    <td>longitude</td>
                    <td>{coords.longitude}</td>
                </tr>
                <tr>
                    <td>altitude</td>
                    <td>{coords.altitude}</td>
                </tr>
                <tr>
                    <td>heading</td>
                    <td>{coords.heading}</td>
                </tr>
                <tr>
                    <td>speed</td>
                    <td>{coords.speed}</td>
                </tr>
            </tbody>
        </table>
    ) : (
        <div>Getting the location data&hellip; </div>
    );
};

export default Demo;

Hook return value

The values returned from the hook are:

{
    coords: {
        latitude,
        longitude,
        altitude,
        accuracy,
        altitudeAccuracy,
        heading,
        speed,
    },
    timestamp, // timestamp of when the last position was retrieved
    isGeolocationAvailable, // boolean flag indicating that the browser supports the Geolocation API
    isGeolocationEnabled, // boolean flag indicating that the user has allowed the use of the Geolocation API
    positionError, // object with the error returned from the Geolocation API call
    getPosition, // a callback you can use to trigger the location query manually
}

The coords value is equivalent to the Coordinates object and the positionError is equivalent to the PositionError.

Configuration

The useGeolocated hook takes optional configuration parameter:

{
    positionOptions: {
        enableHighAccuracy: true,
        maximumAge: 0,
        timeout: Infinity,
    },
    watchPosition: false,
    userDecisionTimeout: null,
    suppressLocationOnMount: false,
    geolocationProvider: navigator.geolocation,
    isOptimisticGeolocationEnabled: true,
    watchLocationPermissionChange: false,
    onError,
    onSuccess,
}

The positionOptions object corresponds to the PositionOptions of the Geolocation API.

By default the hook only sets position once. To watch the user's position and provide live updates to position, set watchPosition = true. The geolocation event handler is unregistered when the hook unmounts.

If set, the userDecisionTimeout determines how much time (in milliseconds) we give the user to make the decision whether to allow to share their location or not. In Firefox, if the user declines to use their location, the Geolocation API call does not end with an error. Therefore we want to fallback to the error state if the user declines and the API does not tell us.

The location is obtained when the hook mounts by default. If you want to prevent this and get the location later, set the suppressLocationOnMount to true and use the getPosition function returned by the hook to trigger the geolocation query manually.

The geolocationProvider allows to specify alternative source of the geolocation API. This was added mainly for testing purposes, however feel free to use it if need be.

The isOptimisticGeolocationEnabled allows you to set the default value of isGeolocationEnabled. By default it is true, which means isGeolocationEnabled will be true on first render. There may be cases where you don't want to assume that the user will give permission, ie you want the first value to for isGeolocationEnabled to be false. In that case, you can set isOptimisticGeolocationEnabled to false.

The watchLocationPermissionChange allows you to watch for changes in the geolocation permissions on browsers that support the permissions API. When set to true, the hook will set a watch on the geolocation permission so that when this permission changes, the location will be obtained again unless the suppressLocationOnMount is also set to true.

The onError callback is called when the geolocation query fails or when the time for the user decision passes. The onSuccess is called when the geolocation query succeeds.

Browser support

The package supports all the browsers with ES6 support (i.e. any modern browser). If you need to support IE11, stick to version < 4 of this package.

Acknowledgements

Many thanks belong to @mcumpl for the original idea for this as well as many suggestions and comments.

This project uses the react-component-boilerplate.

License

react-geolocated is available under MIT. See LICENSE for more details.