我如何将最低的int连接到我的DB, Xamarin表单中的正确名称

本文关键字:表单 Xamarin DB 连接 int 我的 | 更新日期: 2023-09-27 18:09:26

我目前正在使用不同位置之间的lat, lng和收集两个不同点之间的距离。我成功地得到了不同点之间的距离,我目前按最近的距离(以公里为单位)对它们进行排序。

然而,我正在努力使正确的名称达到正确的距离(这是由两个不同的lat组成的,与名称在同一行)。

那么db中的一行看起来是这样的(我有大约10行不同的位置,有正确的纬度,经度):

后期----------- Lng -------名称

45.032——14.323112——冰岛

然后我取这个Lat, Lng,并使用下面的代码获得用户当前位置Lat, Lng和I,然后比较用户当前位置Lat, Lng之间的距离,并从中得到一个包含到最近距离的公里的int。

我现在如何将这个距离与正确的名称联系起来?

    string thename;
    string areaLat;
    string areaLng;
    double storeLat;
    double storeLng;
    double thedistancebetweenobjects;      
    async void loadareas()
    {
        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;
        var position = await CrossGeolocator.Current.GetPositionAsync(10000); //using a plugin that gives me the users current lat, lng. 
        List <int> theInts = new List<int>();
        var getarea = await phpApi.getAreas(); //my db-callout
        foreach (var myitems in getarea["results"])
        {
            thename = myitems["Name"].ToString();
            areaLat = myitems["Lat"].ToString();
            areaLng = myitems["Lng"].ToString();
            storeLat = Double.Parse(areaLat, CultureInfo.InvariantCulture);
            storeLng = Double.Parse(areaLng, CultureInfo.InvariantCulture);
            thedistancebetweenobjects = distance(position.Latitude, position.Longitude,storeLat, storeLng, 'K'); //so the users current position lat + lng, and then the lat (storelat), and lng (storelng) i get from the db.
            int someOtherInt = Convert.ToInt32(thedistancebetweenobjects); //converting double to int
            theInts.Add(someOtherInt);
        }
        int TheSmallestInt = theInts.Min();
        System.Diagnostics.Debug.WriteLine(TheSmallestInt);
        //I succesfully get the smallest int out. But how do I connect this int with the correct name from the db?
    }

这个函数取lat和lng的两个不同值,并给出以千米为单位的距离结果:

private double distance(double lat1, double lon1, double lat2, double lon2, char unit)
    {
        double theta = lon1 - lon2;
        double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) +
        Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
        dist = Math.Acos(dist);
        dist = rad2deg(dist);
        dist = dist * 60 * 1.1515;
        if (unit == 'K')
        {
            dist = dist * 1.609344;
        }
        else if (unit == 'N')
        {
            dist = dist * 0.8684;
        }
        return (dist);
    }
    private double deg2rad(double deg)
    {
        return (deg * Math.PI / 180.0);
    }
    private double rad2deg(double rad)
    {
        return (rad / Math.PI * 180.0);
    }

我如何将最低的int连接到我的DB, Xamarin表单中的正确名称

string closest = string.Empty;
int dist = int.MaxValue;
foreach (var myitems in getarea["results"])
{
  thename = myitems["Name"].ToString();
  // do all of your calcs here
  int someOtherInt = Convert.ToInt32(thedistancebetweenobjects); 
  // for every loc, check if it is closer than the previous loc
  // if it is, save it's name.  
  if (someOtherInt < dist) {
    dist = someOtherInt;
    closest = thename;
  }
  theInts.Add(someOtherInt);
}

我看你用的是哈弗斯公式,在这种情况下,我用的是正确的方法。

我要做的就是循环你的位置列表直到你找到最近的那个

List<double> distList = new List<double>(); //Create a list to store distance values
List<cityLocations> locList = new List<cityLocations>();
distList.Add(50000000); // default huge distance to compare against
foreach (var city in cityList)
{
    //diff between longitudes
    double theta = (double)ci.longitude - (double)ll.longitude;
    //harversine implimentation
    double dist = Math.Sin(Utils.deg2rad(ci.latitude)) * Math.Sin(Utils.deg2rad((double)ll.latitude)) + Math.Cos(Utils.deg2rad(ci.latitude)) * Math.Cos(Utils.deg2rad((double)ll.latitude)) * Math.Cos(Utils.deg2rad(theta));
    dist = Math.Acos(dist);
    dist = Utils.rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (dist < distList[0])
    {
        //always keep 1 record in list
        distList.RemoveAt(0);
        distList.Add(dist);
        if (locList.Count > 0)
        {
            locList.RemoveAt(0);
        }
        locList.Add(new cityLocations { cityName = ll.cityName, latitude = ll.latitude, longitude = ll.longitude, country = ll.country });
    }
}

无论如何都不是最漂亮或最有效的方法