https://github.com/termosa/use-geo
Make use of browser geo location API
https://github.com/termosa/use-geo
Last synced: 4 months ago
JSON representation
Make use of browser geo location API
- Host: GitHub
- URL: https://github.com/termosa/use-geo
- Owner: termosa
- Created: 2020-11-15T20:02:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-11-19T03:00:37.000Z (over 1 year ago)
- Last Synced: 2025-07-28T09:46:55.979Z (11 months ago)
- Language: TypeScript
- Homepage:
- Size: 676 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# use-geo
> Make use of browser geo location API
[](https://www.npmjs.com/package/use-geo) [](https://standardjs.com)
## Install
```bash
npm install --save use-geo
```
## Usage
### Manual position request
```tsx
import * as React from 'react';
import { useGeo } from 'use-geo';
// Or import useGeo from 'use-geo';
const stringifyResults = (status, position, error) => {
if (error) return `Error(${error.message})`;
if (position) return `{\n lat: ${position.coords.latitude},\n long: ${position.coords.longitude}\n}`;
return status;
};
export default () => {
const { status, position, error, request } = useGeo(/* immediate flag (boolean) or PositionOptions object */);
return (
useGeo(): {stringifyResults(status, position, error)}
);
};
```
### Watching position changes
```tsx
import * as React from 'react';
import { useGeoWatch } from 'use-geo';
const stringifyResults = (position, error) => {
if (error) return `Error(${error.message})`;
if (position) return `{\n lat: ${position.coords.latitude},\n long: ${position.coords.longitude}\n}`;
return 'No results';
};
export default () => {
const { position, error, watching, watch, unwatch } = useGeoWatch(/* immediate flag (boolean) or PositionOptions object */);
return (
{watching ? 'watching' : 'not watching'}
useGeo(): {stringifyResults(position, error)}
);
};
```
## License
MIT © [termosa](https://github.com/termosa)
---
This hook is created using [create-react-hook](https://github.com/hermanya/create-react-hook).