首页 > 解决方案 > Xamarin.Forms:无需 API 调用即可更快地显示最近位置?

问题描述

我有一个简单的Xamarin.Forms应用程序,它根据我当前的位置显示最近的沃尔玛商店列表。

假设我的 SQL 数据库有 200 多行;每行都有我所在德克萨斯州的沃尔玛商店的坐标。

所以我最初的逻辑是检索我的位置与所有 200 多个沃尔玛位置之间的距离,如下所示:

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
}

然后,一旦walmartList有了我的位置与所有 200 多家沃尔玛之间的距离,我将在我ListView的位置中显示距离小于 10 英里的位置。

所以,我的问题是:如何限制对 Google 地图的 API 调用量?现在,我需要查询 Google 地图 200 多次,然后显示指定距离(即 10 英里)内的商店。

如果我目前在德克萨斯州休斯顿的位置是12 Oaks Dr,那么我为什么需要获取德克萨斯州所有商店的位置?但我不知道如何在不调用 Google Maps API 并获取实际距离的情况下限制搜索。

标签: google-mapsxamarinxamarin.formsvisual-studio-2017xamarin.forms.listview

解决方案


推荐阅读