Location

The Location library provides functionality for retrieving the location of a device using different positioning methods such as:

  • GNSS satellite positioning including Assisted GNSS (A-GNSS) and Predicted GPS (P-GPS) data.

  • Cellular positioning.

  • Wi-Fi positioning.

Overview

This library provides an API for applications to request the location of a device. The application can determine the preferred order of the location methods to be used along with other configuration information. If a method fails to provide the location, the library performs a fallback to the next preferred method.

Both cellular and Wi-Fi positioning detect the base stations and use web services for retrieving the location. GNSS positioning uses satellites to compute the location of the device. This library can use the assistance data (A-GNSS and P-GPS) to find the satellites faster.

Implementation

The Location library has a compact API and a location core that handles the functionality that is independent of the location method, such as fallback to the next preferred method and timeouts. The supported location methods are as follows:

  • GNSS positioning

  • Cellular positioning

    • Uses LTE link control for getting a list of nearby cellular base stations.

    • Neighbor cell measurement is performed with LTE_LC_NEIGHBOR_SEARCH_TYPE_EXTENDED_LIGHT search type. If more than one cell is requested, a GCI search with LTE_LC_NEIGHBOR_SEARCH_TYPE_GCI_DEFAULT search type is performed to find the cells based on the history information. If a sufficient number of cells still has not been found, a GCI search with LTE_LC_NEIGHBOR_SEARCH_TYPE_GCI_EXTENDED_LIGHT search type is performed. For more details on GCI search, see location_cellular_config.cell_count.

    • The cloud location method handles sending cell information to the selected location service and getting the calculated location back to the device.

  • Wi-Fi positioning

    • Uses Zephyr’s Network Management API Network Management for getting a list of nearby Wi-Fi access points.

    • The cloud location method handles sending access point information to the selected location service and getting the calculated location back to the device.

The cloud location method handles the location methods (cellular and Wi-Fi positioning) that scan for technology-specific information and sends it over to the cloud service for location resolution. If the following conditions are met, Wi-Fi and cellular scan results are combined into a single cloud request:

  • Methods are one after the other in the location request method list.

  • Location request mode is LOCATION_REQ_MODE_FALLBACK.

  • Requested cloud service for Wi-Fi and cellular is the same.

A special LOCATION_METHOD_WIFI_CELLULAR method can appear within the location_event_data structure, but it cannot be added into the location configuration passed to the location_request() function.

The default priority order of location methods is GNSS positioning, Wi-Fi positioning and Cellular positioning. If any of these methods are disabled, the method is simply omitted from the list.

Here are details related to the services handling cell information for cellular positioning, or access point information for Wi-Fi positioning:

Diagrams

In this section, the diagrams illustrate the use of the Location library when the CONFIG_LOCATION_SERVICE_EXTERNAL Kconfig option is set.

The following diagram shows the basic case when GNSS fix is found.

msc {
hscale="1.3";

Application,
Loclib [label="Location library"],
Cloud;

Application => Loclib [label="location_request(&config)\nmethod list: GNSS, Cellular"];
|||;
Application << Loclib [label="LOCATION_EVT_GNSS_ASSISTANCE_REQUEST"];
Application => Cloud [label="Request A-GNSS data"];
Application << Cloud [label="A-GNSS data"];
Application => Loclib [label="location_agnss_data_process()"];
Loclib rbox Loclib [label="Search for GNSS fix"];
...;
Loclib rbox Loclib [label="GNSS fix found"];

Application << Loclib [label="LOCATION_EVT_LOCATION\n(latitude,longitude)"];
}

The following diagram shows successful cellular positioning.

msc {
hscale="1.3";

Application,
Loclib [label="Location library"],
Cloud;

Application => Loclib [label="location_request(&config)\nmethod list: Cellular"];
Loclib rbox Loclib [label="Scan LTE networks"];
|||;
Application << Loclib [label="LOCATION_EVT_CLOUD_LOCATION_EXT_REQUEST"];
Application => Cloud [label="Send LTE cell info, wait for response"];
|||;
Application << Cloud [label="(latitude,longitude)"];
Application => Loclib [label="location_cloud_location_ext_result_set(LOCATION_EXT_RESULT_SUCCESS)\n(latitude,longitude)"];
|||;
Application << Loclib [label="LOCATION_EVT_LOCATION\n(latitude,longitude)"];
|||;
}

The following diagram depicts a failure to find the GNSS fix, followed by a fallback to cloud positioning. Since the Wi-Fi and cellular positioning methods are one after another, they are combined to a single cloud positioning request. Both Wi-Fi APs and LTE cells are given to the application with a single LOCATION_EVT_CLOUD_LOCATION_EXT_REQUEST event. The LOCATION_EVT_STARTED and the LOCATION_EVT_FALLBACK events are sent, if the CONFIG_LOCATION_DATA_DETAILS Kconfig option is set.

msc {
hscale="1.3";

Application,
Loclib [label="Location library"],
Cloud;

Application => Loclib [label="location_request(&config)\nmethod list: GNSS, Wi-Fi, Cellular"];
|||;
Application << Loclib [label="LOCATION_EVT_STARTED"];
Application << Loclib [label="LOCATION_EVT_GNSS_ASSISTANCE_REQUEST"];
Application => Cloud [label="Request A-GNSS data"];
Application << Cloud [label="A-GNSS data"];
Application => Loclib [label="location_agnss_data_process()"];
Loclib rbox Loclib [label="Search for GNSS fix"];
...;
Loclib rbox Loclib [label="GNSS timeout occurs"];

Loclib rbox Loclib [label="Fallback to cloud positioning"];
Application << Loclib [label="LOCATION_EVT_FALLBACK"];
Loclib rbox Loclib [label="Scan Wi-Fi and LTE networks"];
|||;
Application << Loclib [label="LOCATION_EVT_CLOUD_LOCATION_EXT_REQUEST"];
Application => Cloud [label="Send Wi-Fi AP and LTE cell info but do not wait for response"];
Application => Loclib [label="location_cloud_location_ext_result_set(\nLOCATION_EXT_RESULT_UNKNOWN)"];
|||;
Loclib rbox Loclib [label="All methods tried,\nfallback is not possible"];
Application << Loclib [label="LOCATION_EVT_RESULT_UNKNOWN"];
|||;
}

The following diagram illustrates a case where Wi-Fi is used as first priority location method, and its result is unknown. A fallback is performed to GNSS.

msc {
hscale="1.3";

Application,
Loclib [label="Location library"],
Cloud;

Application => Loclib [label="location_request(&config)\nmethod list: Wi-Fi, GNSS, Cellular"];
Loclib rbox Loclib [label="Scan Wi-Fi networks"];
|||;
Application << Loclib [label="LOCATION_EVT_CLOUD_LOCATION_EXT_REQUEST"];
Application => Cloud [label="Send Wi-Fi APs but do not wait for response"];
Application => Loclib [label="location_cloud_location_ext_result_set(LOCATION_EXT_RESULT_UNKNOWN)"];
Loclib rbox Loclib [label="Perform fallback to GNSS"];

Loclib rbox Loclib [label="A-GNSS data already exists\nSearch for GNSS fix"];
...;
Loclib rbox Loclib [label="GNSS fix found"];

Application << Loclib [label="LOCATION_EVT_LOCATION\n(latitude,longitude)"];
}

Requirements

nRF Cloud certificates

When using nRF Cloud for any location data, you must have the certificate provisioned. See Updating the nRF Cloud certificate for more information. An nRF91 Series DK comes pre-provisioned with certificates for nRF Cloud.

Location service accounts

To use the location services that provide A-GNSS or P-GPS, cellular or Wi-Fi positioning data, see the respective documentation for setting up your account and getting the required credentials for authentication:

You can configure the required credentials for the location services using Kconfig options.

Wi-Fi chip

None of the supported DKs have an integrated Wi-Fi chip. You can use an external Wi-Fi chip, such as nRF7002 EK, and connect it to the nRF91 Series DK.

Library files

This library is found under lib/location in the nRF Connect SDK folder structure.

Configuration

Configure the following Kconfig options to enable this library:

Configure the following Kconfig options to enable Wi-Fi interface:

The chosen Wi-Fi device needs to be set in devicetree:

chosen {
  ncs,location-wifi = &mywifi;
};

Configure the following options to enable location methods of your choice:

The following options control the use of GNSS assistance data:

The following option is useful when setting CONFIG_NRF_CLOUD_AGNSS_FILTERED:

The obstructed visibility feature is based on the fact that the number of satellites found indoors or in other environments with limited sky-view is severely decreased. The following options control the sensitivity of obstructed visibility detection:

These options set the threshold for how many satellites need to be found in how long a time period in order to conclude that the device is likely not indoors. Configuring the obstructed visibility detection is always a tradeoff between power consumption and the accuracy of detection.

The following options control the transport method used with nRF Cloud:

Both cellular and Wi-Fi location services are handled externally by the application or selected using the runtime configuration, in which case you must first configure the available services. Use at least one of the following sets of options:

The following options are related to the HERE service and can usually have the default values:

The following options control the default location request configurations and are applied when location_config_defaults_set() function is called:

The following option adds more details to the location_event_data structure:

Usage

To use the Location library, perform the following steps:

  1. Initialize the library with the location_init() function.

  2. Create the configuration (location_config structure).

  3. Set the default values by passing the configuration to the location_config_defaults_set() function together with the list of method types.

  4. Set any required non-default values to the structures.

  5. Call the location_request() function with the configuration.

You can use the location_request() function in different ways, as in the following examples.

Use default values for location configuration:

int err;

err = location_request(NULL);

Use GNSS and cellular and set custom timeout values for them:

int err;
struct location_config config;
enum location_method methods[] = {LOCATION_METHOD_GNSS, LOCATION_METHOD_CELLULAR};

location_config_defaults_set(&config, ARRAY_SIZE(methods), methods);

/* Now you have default values set and here you can modify the parameters you want */
config.timeout = 180 * MSEC_PER_SEC;
config.methods[0].gnss.timeout = 90 * MSEC_PER_SEC;
config.methods[1].cellular.timeout = 15 * MSEC_PER_SEC;

err = location_request(&config);

Use method priority list defined by Kconfig options and set custom timeout values for entire location_request() operation and cellular positioning:

int err;
struct location_config config;

location_config_defaults_set(&config, 0, NULL);

/* Now you have default values set and you can modify the parameters you want but you
 * need to iterate through the method list as the order is defined by Kconfig options.
 */
for (int i = 0; i < config.methods_count; i++) {
    if (config.methods[i].method == LOCATION_METHOD_CELLULAR) {
        config.methods[i].cellular.timeout = 15 * MSEC_PER_SEC;
    }
}

err = location_request(&config);

Samples using the library

The following nRF Connect SDK applications and samples use this library:

Limitations

  • The Location library can only have one application registered at a time. If there is already an application handler registered, another initialization will override the existing handler.

Dependencies

This library uses the following nRF Connect SDK libraries:

It uses the following sdk-nrfxlib library:

It uses the following Zephyr libraries:

API documentation

Header file: include/modem/location.h
Source files: lib/location/
Location library