【问题标题】:how to get a distance between my current location to every found nearby location using xamarin forms如何使用 xamarin 表单获取我当前位置与附近找到的每个位置之间的距离
【发布时间】:2018-12-06 14:48:48
【问题描述】:

我正在开发一个小型应用程序,以使用 Visual Studio 2017 中的 xamarin 表单从当前位置搜索附近的位置。我在 sqlite 中有公司地址列表,例如,我在输入框中输入类似“Perungudi”的内容列表视图中填充了匹配的附近查询“Perungudi”的列表,这是来自 sqlite 的记录,我的问题是“如何获取我当前位置与每个找到的附近位置之间的距离”(距离如 1 公里或 2 公里和公司名称),请给我建议以解决此问题,以便我可以正确搜索

【问题讨论】:

标签: xamarin.forms visual-studio-2017


【解决方案1】:

假设您的 SQLite 记录包含一个位置,您可以使用 Xamarin.Essentials 中的 CalculateDistance 来获取该值。这是一种扩展方法,您可以在此处阅读更多信息:https://docs.microsoft.com/en-us/dotnet/api/xamarin.essentials.locationextensions.calculatedistance

以下内容应该可以帮助您理解该方法:

// Your DB Model
class SavedLocation
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public string Name { get; set; }
}

// Class for binding in the ListView
class LocationWithDistance
{
    public string Name { get; set; }
    public double Distance { get; set; }
}

class SomeViewModel
{
    public IList<LocationWithDistance> FetchNearbyLocations(string name)
    {
        var currentLocation = new Location(19, -99); // This is your current location
        var sqliteData = new List<SavedLocation>(); // Data you get from SQLite

        var itemsForListView = from item in sqliteData
                               select new LocationWithDistance
                               {
                                   Name = item.Name,
                                   Distance = new Location(item.Latitude, item.Longitude).CalculateDistance(currentLocation, DistanceUnits.Kilometers)
                               };

        return itemsForListView.ToList();
    }
}

【讨论】:

  • 感谢您的快速回复,这就是我想要的,我正在努力,如果我有任何疑问,请告诉您
  • 嗨胡安。我有一个疑问,关于 latlng 的计算只是关于距离,同样我需要行驶距离
  • 为此,您需要使用第 3 方服务。 Google 的距离矩阵 API 正是这样做的。您可以指定您想要的旅行模式:驾驶(默认)、步行、骑自行车或公交。详情请看这里:developers.google.com/maps/documentation/distance-matrix/…
猜你喜欢
  • 2016-06-07
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多