> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nerves-hub.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Geo extension

> Report a device's location to NervesHub with the Geo extension, using GeoIP or your own resolver.

The Geo extension answers NervesHub's request for the device's location. By default it does a GeoIP lookup from the device's public IP address. If your device has GPS or a modem that can locate itself, you can supply your own resolver.

## Requirements

* `nerves_hub_link` 2.6.0 or later.
* NervesHub 2.3.0 or later.
* To draw the location on a map, the instance needs a Mapbox access token. Without one, the device page has no **Location** card. See [Feature prerequisites](https://docs.nerves-hub.org/hosting/feature-prerequisites.md#device-location-map).

## Enable it

**1. Keep Geo in your firmware**

`NervesHubLink.Extensions.Geo` is in the default `extension_modules` list. If you set `extension_modules` yourself, that list replaces the default, so name Geo in it:

```elixir theme={null}
config :nerves_hub_link,
  extension_modules: [
    NervesHubLink.Extensions.Geo,
    NervesHubLink.Extensions.Health,
    NervesHubLink.Extensions.NetworkIdentity
  ]
```

**2. Turn it on for the product**

In **Product settings**, on the **General** tab, switch on **Geo** in the **Device extensions** card. A product created in the web UI starts with every extension on. A product created through the API or `nh` starts with every extension off.

**3. Check the device setting**

On the device's **Settings** tab, **Geo** in the **Extensions** card is on by default.

## Check that it works

Reconnect the device if it was connected before you turned Geo on. Open the device's **Details** tab and look at the **Location** card. It shows the map and where the location came from, for example **Approximate, from GeoIP**.

If the resolver returned an error, the card shows the error code and description in place of the map.

## The default resolver

`NervesHubLink.Extensions.Geo.DefaultResolver` asks the Nerves project's [whenwhere](https://whenwhere.nerves-project.org/) service for a rough location based on the device's public IP address. It reports the result with `source: "geoip"`.

The Nerves team provides no availability guarantee for that service.

## Use your own resolver

Implement the `NervesHubLink.Extensions.Geo.Resolver` behaviour. It has one callback, `resolve_location/0`:

```elixir theme={null}
defmodule MyApp.GPSGeoResolver do
  @behaviour NervesHubLink.Extensions.Geo.Resolver

  @impl true
  def resolve_location() do
    case MyApp.GPS.fix() do
      {:ok, %{lat: latitude, lon: longitude}} ->
        {:ok, %{latitude: latitude, longitude: longitude, source: "gps"}}

      {:error, reason} ->
        {:error, "NO_FIX", inspect(reason)}
    end
  end
end
```

Then configure it:

```elixir theme={null}
config :nerves_hub_link,
  geo: [
    resolver: MyApp.GPSGeoResolver
  ]
```

A successful result is a map with `latitude`, `longitude` and `source`, and optionally `accuracy`. NervesHub labels the sources `geoip`, `gps` and `manual`. Any other source is shown as **From** followed by the source name.

> [!NOTE]
>
> Return errors as `{:error, code, description}`. That is the shape the extension in `nerves_hub_link` 2.14.0 matches on and the shape the default resolver returns.

## When the location is sent

NervesHub asks for the location once, each time the extension attaches. That is the default: the repeat interval is an instance setting (`FEATURES_GEO_INTERVAL_MINUTES`) and is `0`, which means no repeat.

## Set a location by hand

A user who can update the device can override the reported location from the **Location** card, with **Manually set the location** or **Manually update location**.

## Related pages

* [Extensions overview](https://docs.nerves-hub.org/extensions/overview.md)
* [Fleet overview](https://docs.nerves-hub.org/management/fleet-overview.md)
* [Extensions protocol](https://docs.nerves-hub.org/device-api/extensions.md)
* [Device location map prerequisite](https://docs.nerves-hub.org/hosting/feature-prerequisites.md#device-location-map)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.