0

I have a simple Xamarin.Forms app that displays a list of the closest Walmart stores based on my current location.

Let's say my SQL database has 200+ rows; each row has the coordinates of a Walmart store in my state of Texax.

So my initial logic was to retrieve the distance between my location and all 200+ Walmart locations, something like this:

Datatable walmartTable = GetAllWalmarts();
string distanceUrl = "https://maps.google.com/maps/api/distancematrix/xml?origins=MyLocationX,MyLocationY&destinations={2},{3}&mode=driving";

List<Walmarts> walmartList=new List<Walmarts>();

foreach(DataRow row in walmartTable.Rows)
{
    x_walmart = row["x];
    y_walmart = row["y];
    currentWalmartName = row["name"];
    // Build distanceUrl variable with x_walmart & y_walmart
    // Call distanceUrl API that returns "distance in miles"
    // Add "distance in miles" to an array, something like this: list.add(currentWalmartName, x_walmart, y_walmart, "distance in miles");
    // Go to the next Walmart store
}

Then, once walmartList has the distance between my location and all 200+ walmarts, I will display in my ListView the locations with a distance of less than 10 miles.

So, my question is: how can I limit the amount of API calls to Google Maps? Right now, I would need to query Google Maps 200+ times and then display the stores that fall within the distance specified (ie. 10 miles).

If my current location in Houston, Texas is 12 Oaks Dr, then why would I need to get the locations of all stores in Texas? But I don't know how to limit that search without calling the Google Maps API and getting the actual distance.

fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110
  • Assuming you are talking about the as-the-crow-flies distance, you can do that directly in a SQLite query (via a SQLite UDF (user defined function), that performs the latlng distance calc), ie: https://stackoverflow.com/questions/41409699/custom-sqlite-functions-in-xamarin-ios/41413561#41413561 Once you have that filtered list of stores, you could then make your google map calls to get the driving distance/directions. – SushiHangover Mar 10 '19 at 03:18
  • is your list of stores stored on the device or is it in a server db? Either way, you can create your own distance function and do a geo query in the db - there is no need to rely on Google for this. Alternately, you can do a pretty simple query that finds all locations within a square centered at your current location. – Jason Mar 10 '19 at 03:34
  • @SushiHangover: If by "as-the-crow-flies" distance you mean a lat-long distance calc, then that solution wouldn't be possible. I have a Walmart that's literally 3 miles from my current distance (with the lat/lon calc), but I have to drive 10 miles to get there because there's no direct road access. – fdkgfosfskjdlsjdlkfsf Mar 10 '19 at 15:16
  • @Jason: the list is in a server database. How would I store the distance information in a DB and how would I calculate that without going to google? – fdkgfosfskjdlsjdlkfsf Mar 10 '19 at 15:20
  • For example, the Google API tells me that there's a distance of 5.48 miles between my current location and _Walmart Boulevard Ave_. How would I be able to store (or calculate) that distance without relying on Google? – fdkgfosfskjdlsjdlkfsf Mar 10 '19 at 15:23
  • @fdkgfosfskjdlsjdlkfsf That is what I was saying, pre-calc the shortest distance, get the closest X number of stores and then obtain the actual driving distance if the user wants it. (this is how store locator apps work, otherwise, yes, you would have to query some service for the driving distance of each of the stores, just not practical) – SushiHangover Mar 10 '19 at 17:37
  • Calculating the distance between two points is basic geometry. For last/long calculations it’s more complicated, but well documented on SO and numerous other places on the web. If you want driving distance then you’re going to have to rely on a service to do that. As Sushi suggests, do a rough distance calc first to narrow down your search, then use the API to calc driving distance – Jason Mar 11 '19 at 00:46
  • If you are storing your stores stations in a MySQL database, you could do a query. – Junior Jiang Mar 11 '19 at 06:56

0 Answers0