我试图在这样的列表中获取几个地址的地理坐标:
private void setRestaurant()
{
foreach (Restaurant restaurant in allRestaurant)
{
GeoCoordinate help;
GeocodeQuery query = new GeocodeQuery()
{
GeoCoordinate = new GeoCoordinate(0, 0),
SearchTerm = restaurant.address
};
query.QueryCompleted += (s, e) =>
{
foreach (var item in e.Result)
{
help = item.GeoCoordinate;
restaurants.Add(restaurant);
}
};
query.QueryAsync();
}
}
由于某些东西它无法获得任何一个地理坐标(有时会找到一个)。地址是正确的,我确信,我一个接一个地尝试了它们没有迭代,所以bug在这个代码中的某个地方。有什么想法吗?
谢谢!
答案 0 :(得分:2)
这就是原因:
foreach (Restaurant restaurant in allRestaurant)
{
GeoCoordinate help;
GeocodeQuery query = new GeocodeQuery()
{
GeoCoordinate = new GeoCoordinate(),
SearchTerm = restaurant.address
};
TaskCompletionSource<Restaurant> task = new TaskCompletionSource<Restaurant>();
query.QueryCompleted += (s, ev) =>
{
foreach (var item in ev.Result)
{
help = item.GeoCoordinate;
task.TrySetResult(restaurant);
}
task.TrySetResult(null);
};
query.QueryAsync();
var rest = (await task.Task);
if (rest != null)
restaurants.Add(rest);
}
似乎您无法运行多个查询,因此您必须在检查另一个地址之前等待。