All Projects → bence-toth → react-hook-geolocation

bence-toth / react-hook-geolocation

Licence: LGPL-3.0 license
A React hook to access data from the Geolocation API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-hook-geolocation

ipinfodb-php
PHP library to query free IPInfoDB API service.
Stars: ✭ 15 (-51.61%)
Mutual labels:  geolocation, geolocation-api
ionic2-firebase-hackathon-starter
A hackathon starter for ionic2 using firebase, already has user authentication, reset password, google login etc
Stars: ✭ 34 (+9.68%)
Mutual labels:  geolocation, geolocation-api
geolocation
A laravel integration for using the IPInfoDB and Ip2Location services
Stars: ✭ 38 (+22.58%)
Mutual labels:  geolocation, geolocation-api
go
Official Golang client library for the ipdata API
Stars: ✭ 21 (-32.26%)
Mutual labels:  geolocation, geolocation-api
ipapi-python
Python bindings for https://ipapi.co (IP Address Location) - Use with python / django / flask for IP address location lookup
Stars: ✭ 42 (+35.48%)
Mutual labels:  geolocation, geolocation-api
aimscroll
🍹 Painless utility libary to handle scroll positions and methods in react
Stars: ✭ 12 (-61.29%)
Mutual labels:  react-hooks
LabsPT1 bkwds
bkwds is an app that helps adventurers track their trips and travel safely
Stars: ✭ 40 (+29.03%)
Mutual labels:  geolocation
IP2Country
Ip to country mapping
Stars: ✭ 39 (+25.81%)
Mutual labels:  geolocation
MovieCards
React App that uses TMDb API to display movie data. Try it out! ->
Stars: ✭ 38 (+22.58%)
Mutual labels:  react-hooks
book-fullstack-react
Fullstack React: The Complete Guide to ReactJS and Friends by Anthony Accomazzo
Stars: ✭ 100 (+222.58%)
Mutual labels:  react-hooks
use-local-storage
A flexible React Hook for using Local Storage.
Stars: ✭ 57 (+83.87%)
Mutual labels:  react-hooks
locus
MMDB reader for geolocation and ASN lookup of IP addresses
Stars: ✭ 93 (+200%)
Mutual labels:  geolocation
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-58.06%)
Mutual labels:  react-hooks
photos2geojson
html map gallery from photos
Stars: ✭ 39 (+25.81%)
Mutual labels:  geolocation
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+109.68%)
Mutual labels:  react-hooks
crisis-news-mapper
日本の災害関連ニュースをTwitterから収集して地図上にマッピングするFirebaseプロジェクト crisis.yuiseki.net
Stars: ✭ 13 (-58.06%)
Mutual labels:  geolocation
php
Official PHP client library for the ipdata API
Stars: ✭ 16 (-48.39%)
Mutual labels:  geolocation
use-fetch-hook
A custom hook to fetch and cache data
Stars: ✭ 95 (+206.45%)
Mutual labels:  react-hooks
Avenue-GPX-Viewer
A simple and easy GPX viewer for macOS.
Stars: ✭ 42 (+35.48%)
Mutual labels:  geolocation
pojo-observer
A minimalist object observer with React hooks support. Allows you to separate concerns between presentation and interaction logic
Stars: ✭ 91 (+193.55%)
Mutual labels:  react-hooks

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 React from 'react'
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)

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].

Licence

LGPL-3.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].