Skip to content
CannonReactHooks

useGeoLocation Hook

Introduction

A custom React hook for accessing and monitoring a user’s geolocation.

Play

Usage

import React from "react";
import { useGeolocation } from "@cannonui/reacthooks";

const GeolocationDisplay = () => {
  const location = useGeolocation();

  if (location.loading) {
    return <div>Loading geolocation data...</div>;
  }

  if (location.error) {
    return <div>Error fetching geolocation data: {location.error.message}</div>;
  }

  return (
    <div>
      <h2>Geolocation Data:</h2>
      <table>
        <tbody>
          <tr>
            <td>Latitude:</td>
            <td>{location.latitude}</td>
          </tr>
          <tr>
            <td>Longitude:</td>
            <td>{location.longitude}</td>
          </tr>
          <tr>
            <td>Altitude:</td>
            <td>{location.altitude}</td>
          </tr>
          <tr>
            <td>Accuracy:</td>
            <td>{location.accuracy}</td>
          </tr>
          <tr>
            <td>Heading:</td>
            <td>{location.heading}</td>
          </tr>
          <tr>
            <td>Speed:</td>
            <td>{location.speed}</td>
          </tr>
          <tr>
            <td>Timestamp:</td>
            <td>{new Date(location.timestamp).toLocaleString()}</td>
          </tr>
        </tbody>
      </table>
    </div>
  );
};

export default GeolocationDisplay;

Props

NameTypeDescription
optionsPositionOptions (optional)An optional configuration object provided when calling useGeolocation. It is used when calling navigator.geolocation.watchPosition() and navigator.geolocation.getCurrentPosition(). Some of the attributes it accepts are enableHighAccuracy, timeout, and maximumAge.

Return Value

The hook returns an object containing the following properties:

NameTypeDescription
loadingbooleanA boolean indicating if the geolocation data is currently being fetched.
accuracynumber | nullThe accuracy of the latitude and longitude properties in meters.
altitudenumber | nullThe altitude in meters above the mean sea level.
altitudeAccuracynumber | nullThe accuracy of altitude in meters.
headingnumber | nullThe direction in which the device is traveling. This value, specified in degrees, indicates how far off from heading true north the device is.
latitudenumber | nullThe latitude in decimal degrees.
longitudenumber | nullThe longitude in decimal degrees.
speednumber | nullThe current ground speed of the device, specified in meters per second.
timestampnumber | nullThe timestamp at which the geolocation data was retrieved.
errorPositionError | nullAn error object, if an error occurred while retrieving the geolocation data.

Features

  • Provides access to the user’s geolocation data, including latitude, longitude, altitude, accuracy, heading, speed, and timestamp.
  • Handles loading state while fetching geolocation data.
  • Handles errors if geolocation data retrieval fails.
  • Continuously monitors geolocation changes if options are provided.

Example

In this example, we have a component called GeolocationDisplay that uses the useGeolocation hook to fetch and display the user’s geolocation data. The component conditionally renders the geolocation data, loading state, or any error message that might occur during data retrieval.

Use Scenario

Suppose you are building a weather app and need to fetch the user’s current location to provide accurate weather information. You can use the useGeolocation hook in your app to access the user’s geolocation and then use the latitude and longitude data to fetch weather data from an external API.

With the GeolocationDisplay component, you can easily obtain the user’s geolocation data and display it on your weather app’s user interface. The component will handle the loading state while fetching the data and display any errors that might occur during the process.

By incorporating the useGeolocation hook into your weather app, you can enhance the user experience by providing location-specific weather information without the need for the user to manually enter their location.