Get the device location
Read the geographic location of the device, such as its latitude and longitude, from the location sensor. Use it to place content on a map or to start events near a real-world place.
Use the LocationSensor device to get the location of the device that your application runs on. The location sensor uses the device's location service, such as GPS. It reports:
- The latitude, longitude, and altitude of the device.
- The accuracy of the reading.
- The time the reading was taken.
The location sensor replaces the legacy Input.location API. Use only one of these APIs in a project. Both use the same platform location service, so if you disable one, the other also stops getting updates.
Prerequisites
To get a location:
- The device must support location services, and location services must be on in the device settings.
- The user must allow your application to access the device location. When you enable the sensor, the operating system shows the permission prompt if the user hasn't decided yet. On the Web platform, the browser shows the prompt, and the page must use HTTPS.
Read the location
Like other sensors, the location sensor is disabled by default. When you enable it, the location service starts in the background. It can take several seconds to get a first reading, so check the status property before you use the readings.
To read the location:
Enable the sensor with
InputSystem.EnableDevice.Check that
statusisLocationServiceStatus.Running. The status is one of the following values:Stopped: The service isn't running, for example before you enable the sensor or after you disable it.Initializing: The service is starting, and the readings aren't valid yet.Running: The service is active, and its readings are valid.Failed: The service can't start, for example because the user denied access.
Read the values from the
latitudeandlongitudecontrols, and from the other controls you need.When you no longer need location updates, disable the sensor with
InputSystem.DisableDevice. The location service uses power while it runs.
Example: Log each new location reading
The following example logs each new location reading while the location service is running:
using UnityEngine;
using UnityEngine.InputSystem;
using LocationServiceStatus = UnityEngine.InputSystem.LocationServiceStatus;
/// <summary>
/// Example script that logs each new location reading from the device.
/// </summary>
public class ReadDeviceLocationExample : MonoBehaviour
{
double m_LastTimestamp;
void OnEnable()
{
// Referencing LocationSensor in code also makes sure that Android and iOS builds
// include the location permission and usage description.
var location = LocationSensor.current;
if (location == null)
return;
InputSystem.EnableDevice(location);
}
void OnDisable()
{
if (LocationSensor.current != null)
InputSystem.DisableDevice(LocationSensor.current);
}
void Update()
{
var location = LocationSensor.current;
if (location == null || !location.enabled)
return;
// The location service starts asynchronously. Readings are valid only while it's running.
var status = location.status;
if (status == LocationServiceStatus.Failed)
{
Debug.LogWarning("The location service failed to start.");
enabled = false;
return;
}
if (status != LocationServiceStatus.Running)
return;
// Log each new reading once.
var timestamp = location.timestamp.ReadValue();
if (timestamp <= m_LastTimestamp)
return;
m_LastTimestamp = timestamp;
Debug.Log($"Location: {location.latitude.ReadValue()}, {location.longitude.ReadValue()} " +
$"(accuracy {location.horizontalAccuracy.ReadValue()} m)");
}
}
Avoid type name conflicts
Both the UnityEngine and UnityEngine.InputSystem namespaces define a LocationServiceStatus type. If your script uses both namespaces, write UnityEngine.InputSystem.LocationServiceStatus in full, as in the example, or add a using alias.
Sensor controls
The location sensor has the following controls:
| Control | Description |
|---|---|
latitude |
Latitude, in degrees. |
longitude |
Longitude, in degrees. |
altitude |
Altitude, in meters. |
horizontalAccuracy |
Horizontal accuracy of the reading, in meters. |
verticalAccuracy |
Vertical accuracy of the reading, in meters. |
timestamp |
Time the reading was taken, in seconds since the epoch that the platform location service uses. |
To change how accurate the readings are and how far the device must move before the sensor reports a new reading, refer to Location settings.
Handle a refused permission
If the user refuses the permission, status changes to Failed and never reaches Running. Check status to detect this, as the example does.
Don't use isEnabledByUser to detect a refusal. On Android and iOS, it tells you whether location services are on for the whole device, not whether your application has permission. On the Web platform, it's false until the first reading arrives.
On Android 12 and later, the user can allow approximate location only. The sensor then runs with lower accuracy.
Include the location permission in your build
On Android and iOS, a build that uses the location service must include a location permission (Android) or a location usage description (iOS). Unity detects this when your compiled scripts reference the LocationSensor type.
- On Android, Unity adds the location permission to the manifest automatically.
- On iOS, enter a text in Player Settings > Other Settings > Location Usage Description. If a script references
LocationSensorand this text is empty, the build fails.
If you access the location sensor only through a binding in an input actions asset, and no script references LocationSensor, the build doesn't include the permission or the usage description, and the location service fails to start at runtime. To fix this, do one of the following:
- Reference
LocationSensorin a script, for example by readingLocationSensor.currentonce. - Add the location permission and usage description to your build manually.