desiredAccuracyInMeters | The service accuracy you want to use, in meters. This determines the accuracy of the device's last location coordinates. Higher values like 500 don't require the device to use its GPS chip and thus save battery power. Lower values like 5-10 provide the best accuracy but require the GPS chip and thus use more battery power. The default value is 10 meters. |
updateDistanceInMeters | The minimum distance, in meters, that the device must move laterally before Unity updates Input.location. Higher values like 500 produce fewer updates and are less resource intensive to process. The default is 10 meters. |
Starts location service updates.
After you call this function, you can access the device's last location coordinates.
To do this, check lastData in Input.location.
Note: The location service doesn't start to send location data immediately. Check status in Input.location for the current service status.
On Android, using this method in a scripts automatically adds the ACCESS_FINE_LOCATION permission to the android manifest. If you use low accuracy values like 500 or higher, you can select Low Accuracy Location in Player Settings to add the ACCESS_COARSE_LOCATION permission instead.
using UnityEngine; using System.Collections;
public class TestLocationService : MonoBehaviour { IEnumerator Start() { // Check if the user has location service enabled. if (!Input.location.isEnabledByUser) yield break;
// Starts the location service. Input.location.Start();
// Waits until the location service initializes int maxWait = 20; while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) { yield return new WaitForSeconds(1); maxWait--; }
// If the service didn't initialize in 20 seconds this cancels location service use. if (maxWait < 1) { print("Timed out"); yield break; }
// If the connection failed this cancels location service use. if (Input.location.status == LocationServiceStatus.Failed) { print("Unable to determine device location"); yield break; } else { // If the connection succeeded, this retrieves the device's current location and displays it in the Console window. print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp); }
// Stops the location service if there is no need to query location updates continuously. Input.location.Stop(); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.