{"id":27559156,"url":"https://github.com/bence-toth/react-hook-geolocation","last_synced_at":"2025-06-22T12:09:26.451Z","repository":{"id":34166054,"uuid":"170939563","full_name":"bence-toth/react-hook-geolocation","owner":"bence-toth","description":"A React hook to access data from the Geolocation API","archived":false,"fork":false,"pushed_at":"2023-01-09T02:24:17.000Z","size":1365,"stargazers_count":39,"open_issues_count":4,"forks_count":13,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-19T23:34:12.227Z","etag":null,"topics":["geolocation","geolocation-api","react","react-hooks"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bence-toth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-15T23:01:01.000Z","updated_at":"2025-02-14T21:36:46.000Z","dependencies_parsed_at":"2023-01-15T05:02:08.558Z","dependency_job_id":null,"html_url":"https://github.com/bence-toth/react-hook-geolocation","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/bence-toth/react-hook-geolocation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bence-toth%2Freact-hook-geolocation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bence-toth%2Freact-hook-geolocation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bence-toth%2Freact-hook-geolocation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bence-toth%2Freact-hook-geolocation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bence-toth","download_url":"https://codeload.github.com/bence-toth/react-hook-geolocation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bence-toth%2Freact-hook-geolocation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261289430,"owners_count":23136071,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["geolocation","geolocation-api","react","react-hooks"],"created_at":"2025-04-19T23:34:06.200Z","updated_at":"2025-06-22T12:09:21.440Z","avatar_url":"https://github.com/bence-toth.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-hook-geolocation :earth_africa:\n\nA React hook to access data from the [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API).\n\n## Installation\n\nUsing `npm`:\n\n```sh\nnpm install --save react-hook-geolocation\n```\n\nUsing `yarn`:\n\n```sh\nyarn add react-hook-geolocation\n```\n\n## Usage\n\n```jsx\nimport useGeolocation from \"react-hook-geolocation\";\n\nconst ComponentWithGeolocation = () =\u003e {\n  const geolocation = useGeolocation();\n\n  return !geolocation.error ? (\n    \u003cul\u003e\n      \u003cli\u003eLatitude: {geolocation.latitude}\u003c/li\u003e\n      \u003cli\u003eLongitude: {geolocation.longitude}\u003c/li\u003e\n      \u003cli\u003eLocation accuracy: {geolocation.accuracy}\u003c/li\u003e\n      \u003cli\u003eAltitude: {geolocation.altitude}\u003c/li\u003e\n      \u003cli\u003eAltitude accuracy: {geolocation.altitudeAccuracy}\u003c/li\u003e\n      \u003cli\u003eHeading: {geolocation.heading}\u003c/li\u003e\n      \u003cli\u003eSpeed: {geolocation.speed}\u003c/li\u003e\n      \u003cli\u003eTimestamp: {geolocation.timestamp}\u003c/li\u003e\n    \u003c/ul\u003e\n  ) : (\n    \u003cp\u003eNo geolocation, sorry.\u003c/p\u003e\n  );\n};\n```\n\n### Using `PositionOptions`\n\n[There is a way](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API#Fine_tuning_response) to use `PositionOptions` to fine tune response coming from `watchPosition` of the Geolocation API.\n\nIf you want to use this feature, simply provide `useGeolocation` with a `PositionOptions` object:\n\n```jsx\nconst geolocation = useGeolocation({\n  enableHighAccuracy: true,\n  maximumAge: 15000,\n  timeout: 12000,\n});\n```\n\n### Using a callback function\n\nYou 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.\n\nIf you don't use `PositionOptions`, I recommend that you supply `{}` as your first argument.\n\n```jsx\nconst onGeolocationUpdate = (geolocation) =\u003e {\n  console.log(\"Here’s some new data from the Geolocation API: \", geolocation);\n};\n\nconst geolocation = useGeolocation({}, onGeolocationUpdate);\n```\n\n### Waiting for the user to opt in\n\nThe 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.\n\nIf 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.\n\n```jsx\nconst geolocation = useGeolocation({}, () =\u003e {}, false);\n```\n\nSetting 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.\n\n## Notes\n\nAccess to data from the Geolocation API needs user permission.\n\nIf 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.\n\nIf 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.\n\n## Caveats\n\nGeolocation API is available only in secure contexts (a.k.a. only using HTTPS).\n\nGetting geolocation data can take time, especially with high accuracy enabled – getting a GPS fix can take up to a minute.\n\n## Contributions\n\nContributions are welcome. File bug reports, create pull requests, feel free to reach out at tothab@gmail.com.\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbence-toth%2Freact-hook-geolocation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbence-toth%2Freact-hook-geolocation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbence-toth%2Freact-hook-geolocation/lists"}