Xamarin-如何通过GPS实时计算距离 [英] Xamarin - How to calculate distance in realtime via GPS
问题描述
我如何通过GPS实时计算距离(如果有运动,距离必须增加)?
How can i calculate distance in real time(the distance must increment if there is a movement) via GPS ?
我有一个显示行驶速度的应用程序,它很准确.我只需要显示用户行进的距离即可.
I have an App that shows the travelling speed, and it is accurate. I just need to show the distance that was traveled by the user.
我该如何存档?请在下面查看我的代码:
How can i archive this ? Please see my code below :
public void OnLocationChanged(Location location)
{
try
{
currentLocation = location;
if (currentLocation == null)
{
//"Make sure the GPS is on.";
}
else
{
gpsLatitude = currentLocation.Latitude.ToString();
gpsLongitude = currentLocation.Longitude.ToString();
if (currentLocation.Speed != 0)
{
kmh = (int)((currentLocation.Speed * 3600) / 1000);
//mph = (int)(currentLocation.Speed * 2.2369);
}
}
}
catch (Exception ex)
{
LogException(ex);
}
}
推荐答案
假设您在用户旅行时存储LatLng点,则以下方法可以满足您的需求.
Assuming you are storing the LatLng points as the user travels, the following methods can provide what you need.
有一个 ComputeDistanceBetween
返回米至 LatLng
点之间的距离,而一个 ComputeLength
返回介于 LatLng
点.
There is a ComputeDistanceBetween
that returns the meters between to LatLng
points and a ComputeLength
that returns the total meters between a sequential list of LatLng
points.
这是基于Google的Android Maps Utils的 SphericalUtil
方法的,并且是我在公共领域中看到的最好的实现之一.
This is based on SphericalUtil
methods from Google's Android Maps Utils and is one of best implementations that I've seen in the public domain.
注意:Google的Java代码在Apache License Version 2.0下,我将其转换为C#.
Note: Google's Java code for that is under Apache License Version 2.0, and I converted it to C#.
var latlng1 = new LatLng(47.61472695767613, -122.33327865600586);
var latlng2 = new LatLng(47.60269078742121, -122.30581283569336);
var latlng3 = new LatLng(47.608593486245546, -122.3001480102539);
var latlngList = new List<LatLng> { latlng1, latlng2, latlng3 };
var km1 = Meters.ComputeDistanceBetween(latlng1, latlng2);
var km2 = Meters.ComputeDistanceBetween(latlng2, latlng3);
var kmTotal = Meters.ComputeLength(latlngList);
实施:
public static class Meters
{
const double EARTH_RADIUS = 6371009;
static double ToRadians(double input)
{
return input / 180.0 * Math.PI;
}
static double DistanceRadians(double lat1, double lng1, double lat2, double lng2)
{
double Hav(double x)
{
double sinHalf = Math.Sin(x * 0.5);
return sinHalf * sinHalf;
}
double ArcHav(double x)
{
return 2 * Math.Asin(Math.Sqrt(x));
}
double HavDistance(double lat1b, double lat2b, double dLng)
{
return Hav(lat1b - lat2b) + Hav(dLng) * Math.Cos(lat1b) * Math.Cos(lat2b);
}
return ArcHav(HavDistance(lat1, lat2, lng1 - lng2));
}
public static double ComputeDistanceBetween(LatLng from, LatLng to)
{
double ComputeAngleBetween(LatLng From, LatLng To)
{
return DistanceRadians(ToRadians(from.Latitude), ToRadians(from.Longitude),
ToRadians(to.Latitude), ToRadians(to.Longitude));
}
return ComputeAngleBetween(from, to) * EARTH_RADIUS;
}
public static double ComputeLength(List<LatLng> path)
{
if (path.Count < 2)
return 0;
double length = 0;
LatLng prev = path[0];
double prevLat = ToRadians(prev.Latitude);
double prevLng = ToRadians(prev.Longitude);
foreach (LatLng point in path)
{
double lat = ToRadians(point.Latitude);
double lng = ToRadians(point.Longitude);
length += DistanceRadians(prevLat, prevLng, lat, lng);
prevLat = lat;
prevLng = lng;
}
return length * EARTH_RADIUS;
}
}
仅供参考:如果您需要以平方米为单位的面积,请在此处查看我的答案,该答案也基于Google的Android Maps Utils代码
FYI: If you need an area in square meters, see my answer here, also based on Google's Android Maps Utils code
这篇关于Xamarin-如何通过GPS实时计算距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!