How To used stored location in database with location calculate distance method

Gbenga Banuso Odumosu 40 Reputation points
2024-05-09T13:07:16.15+00:00

I am working on MAUI to calculate distance between two locations. I have stored the first et of location in an sqlits database. Using Location calculate distance method, I got error Severity Code Description Project Path File Line Suppression State

Error	CS1503	Argument 1: cannot convert from 'double' to 'Microsoft.Maui.Devices.Sensors.Location'

the program

return await conn.Table<Person>().Where(s => s.location == Location.CalculateDistance(abuja,s.location < 3.0, DistanceUnits.Kilometers)).ToListAsync();
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,996 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 58,126 Reputation points
    2024-05-09T16:10:44.5333333+00:00

    you have a couple issues.

    a Location can not be stored in a sqlite database, typically you would store the lat & long, as two double columns.

    second the where expression makes no sense. s.location < 3.0 is a bool, but the Location.CalculateDistance() has no overload with a bool.

    third the Location.CalculateDistance can not be converted to sql, so you would be need to read the table into a memory collection first:

    return (await conn.Table<Person>().ToListAsync())
       .Where(s => 
           Location.CalculateDistance
           (
              s.loctionLat, 
              s.locationLong,
              abuja,
              DistanceUnits.Kilometers
           ) < 3.0
       ),
       .ToListAsync();
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful