Reputation: 957
I'm developing a small application to search a nearby location from current location using xamarin forms in visual studio 2017. I am having list of company address in sqlite, For examble I type something like "Perungudi" in the Entry box then the listview filled with the list of matched nearby query "Perungudi", Which is record coming from sqlite, My question is "how to get a distance between my current location to every found nearby location" (distance like 1 km or 2 km and Company name), Please give me suggestion to resolve this issue so that i can search right way
Upvotes: 0
Views: 464
Reputation: 41
Assuming your SQLite records include a location, you can use CalculateDistance from Xamarin.Essentials to obtain the value. This is an extension method, you can read more about it here: https://learn.microsoft.com/en-us/dotnet/api/xamarin.essentials.locationextensions.calculatedistance.
The following should help you understand the approach:
// 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();
}
}
Upvotes: 2