Carlos Rodrigez - 1 year ago 69

C# Question

I am currently working with lat, lng between different locations and gathering the distance between two different points.

I succesfully get out the distance between different points where I currently sort them by the closest distance (in kilometer).

What I however am struggeling with is getting the correct name to the correct distance (that is being made out of the two different lat, lngs that is in the same row as the name).

So one row in the db looks like this (I have like 10 different rows with different locations with their correct latitude, longtitud):

Lat ----------- Lng ------- Name

45.032 --14.323112-----Iceland

I then take that Lat, Lng and with my code below I get the users current position lat, lng and i then compare the distance between the users current lat, lng position and out of it i have an int cointaining the kilometer to the closest distance.

How can I now connect that distance with the correct Name?

`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?

}

And this is the function that takes the two different values of lat, lng and gives me the distance result in kilometer:

`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);

}

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
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);
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**