我有一个带有开始和停止按钮的应用程序。当用户点击开始时,它会打开 GPS。当用户点击停止时,它会关闭 GPS。我想计算用户从开始到停止的距离。
这是我到目前为止的代码,但它不起作用。
我正在打印点 1 和点 2 的 GPS 坐标,四舍五入到小数点后 4 位。Update()
即使它为点 1 和点 2 打印相同的 GPS 坐标,它也会进入 if 语句。
另一个问题是,即使我没有移动,只要按下开始按钮,它就会显示我已经行驶了约 1200 公里。
public double long1 = 0;
public double lat1 = 0;
public double long2 = 0;
public double lat2 = 0;
void Start ()
{
distance_label = distanceObject.GetComponent<Text> ();
// *** DELETE WHEN DONE - text labels for testing
testP1 = p1Object.GetComponent<Text> ();
testP2 = p2Object.GetComponent<Text> ();
testMsg = test.GetComponent<Text> ();
distance_label.text = "You and your blobs have walked " + distanceWalked + " km.";
}
void Update ()
{
GetCurrentLocation ();
double latRounded1 = Math.Round (lat1, 4);
double longRounded1 = Math.Round (long1, 4);
double latRounded2 = Math.Round (lat2, 4);
double longRounded2 = Math.Round (long2, 4);
testP1.text = "<Point 1 Coordinates> Lat: " + latRounded1 + ", Long: " + longRounded1;
testP2.text = "<Point 2 Coordinates> Lat: " + latRounded2 + ", Long: " + longRounded2;
// Compare GPS coordinates rounded to 4 decimal places
if (latRounded1 != latRounded2 || longRounded1 != longRounded2) {
testMsg.text = "Lat/Long don't match.";
distanceWalked += CalculateDistance (longRounded1, latRounded1, longRounded2, latRounded2);
distance_label.text = "You and your blobs have walked " + distanceWalked + " km.";
long1 = long2;
lat1 = lat2;
}
}
/// <summary>
/// Makes a call to start GPS service when Start button is clicked.
/// </summary>
public void StartButtonClicked ()
{
Debug.Log ("Start button clicked...");
StartCoroutine (StartLocationService ());
}
/// <summary>
/// Stops GPS service when Stop button is clicked.
/// </summary>
public void StopButtonClicked ()
{
Debug.Log ("Stop button clicked...");
Input.location.Stop ();
distance_label.text = "Stopped.";
}
public void ResetButtonClicked()
{
Debug.Log("Reset button clicked...");
distanceWalked = 0;
Input.location.Stop ();
Debug.Log ("distanceWalked after reset: " + distanceWalked);
}
public void GetCurrentLocation()
{
if (Input.location.status == LocationServiceStatus.Running) {
long2 = Input.location.lastData.longitude;
lat2 = Input.location.lastData.latitude;
}
}
/// <summary>
/// Starts the location service.
/// </summary>
IEnumerator StartLocationService ()
{
// First, check if user has location service enabled
if (!Input.location.isEnabledByUser)
//yield break;
distance_label.text = "Not initialized";
// Start service before querying location
Input.location.Start (0.5f); // accuracy = 0.5m
// Wait until service initializes
int maxWait = 20;
while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0) {
yield return new WaitForSeconds (1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1) {
distance_label.text = "Timed out";
yield break;
}
// Connection has failed
if (Input.location.status == LocationServiceStatus.Failed) {
distance_label.text = "Unable to determine device location";
yield break;
} else {
long1 = long2 = Input.location.lastData.longitude;
lat1 = lat2 = Input.location.lastData.latitude;
// Access granted and location value could be retrieved
// distance_label.text = "Location: " + Input.location.lastData.latitude + " " +
// Input.location.lastData.longitude + " " +
// Input.location.lastData.altitude;
}
// Stop service if there is no need to query location updates continuously
//Input.location.Stop ();
}
/// <summary>
/// Calculate distance traveled given two points.
/// </summary>
/// <param name="longitude1"> Longitude coordinate of point 1</param>
/// <param name="latitude1"> Latitude coordinate of point 1</param>
/// <param name="longitude2"> Longitude coordinate of point 2</param>
/// <param name="latitude2"> Latitude coordinate of point 2</param>
public double CalculateDistance (double longitude1, double latitude1, double longitude2, double latitude2)
{
double radiansOverDegrees = (Math.PI / 180.0);
double EARTH_MEAN_RADIUS = 6371; // in km
double radiansLong1 = longitude1 * radiansOverDegrees;
double radiansLat1 = latitude1 * radiansOverDegrees;
double radiansLong2 = longitude2 * radiansOverDegrees;
double radiansLat2 = latitude2 * radiansOverDegrees;
double dLong = radiansLong2 - radiansLong1;
double dLat = radiansLat2 - radiansLat1;
double dist1 = Math.Pow (Math.Sin (dLat / 2.0), 2.0) +
Math.Cos (radiansLat1) * Math.Cos (radiansLat2) *
Math.Pow (Math.Sin (dLong / 2.0), 2.0);
double dist2 = EARTH_MEAN_RADIUS * 2.0 * Math.Atan2 (Math.Sqrt (dist1), Math.Sqrt (1.0 - dist1));
return dist2;
}