Google Place Api Prediction class cast to Position 或类似的东西
Google Place Api Prediction class cast to Position or something equivalent
我正在处理 Xamarin Forms 项目。我有一个非常简单的页面,求职者通过输入位置(例如纽约)和半径(选择器)来搜索职位。我的 PageViewModel 中有以下代码:
public class SearchPageViewModel : BaseViewModel
{
private readonly INavigation _navigation;
private readonly GooglePlacesApiService _api;
private IEnumerable<JobPost> Posts;
public int RadiusIndex {get;set;}
public SearchPageViewModel (INavigation navigation, IEnumerable<JobPost> posts)
{
_navigation = navigation;
var settings = GoogleApiSettings.Builder.WithApiKey("MY_API_KEY").Build();
_api = new GooglePlacesApiService(settings);
this.posts =posts;
}
public ICommand DoSearchCommand
=> new Command(async () => await DoSearchAsync().ConfigureAwait(false), () => CanSearch);
public ICommand SelectItemCommand
=> new Command<Prediction>(async (prediction) =>
{
//!!!Filter jobposts based on position and radius to be able to display it!!!
_api.ResetSessionToken();
});
private string _searchText;
public string SearchText
{
get => _searchText;
set
{
if (SetProperty(ref _searchText, value))
OnPropertyChanged("CanSearch");
}
}
private List<Prediction> _results;
public List<Prediction> Results
{
get => _results;
set => SetProperty(ref _results, value);
}
public bool CanSearch => !string.IsNullOrWhiteSpace(SearchText) && SearchText.Length > 2;
private async Task DoSearchAsync()
{
var results = await _api.GetPredictionsAsync(SearchText)
.ConfigureAwait(false);
if(results != null && results.Status.Equals("OK"))
{
ResultCount = results.Items.Count;
Results = results.Items;
OnPropertyChanged("HasResults");
}
}
}
public class JobPost
{
public Position Location {get;set;}
}
好的,到目前为止效果很好。单击搜索按钮后,求职者将获得一份预测列表并选择一个地方。但现在的问题是,我如何获得所选预测的定位(经度和纬度),以便我可以过滤职位,以便将结果显示给求职者。
提前致谢。
使用 Essentials GeoCoding 插件
var address = "New York";
var locations = await Geocoding.GetLocationsAsync(address);
locations
将是一个潜在匹配的列表,每个都有一个 lat/long
var location = locations?.FirstOrDefault();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
我正在处理 Xamarin Forms 项目。我有一个非常简单的页面,求职者通过输入位置(例如纽约)和半径(选择器)来搜索职位。我的 PageViewModel 中有以下代码:
public class SearchPageViewModel : BaseViewModel
{
private readonly INavigation _navigation;
private readonly GooglePlacesApiService _api;
private IEnumerable<JobPost> Posts;
public int RadiusIndex {get;set;}
public SearchPageViewModel (INavigation navigation, IEnumerable<JobPost> posts)
{
_navigation = navigation;
var settings = GoogleApiSettings.Builder.WithApiKey("MY_API_KEY").Build();
_api = new GooglePlacesApiService(settings);
this.posts =posts;
}
public ICommand DoSearchCommand
=> new Command(async () => await DoSearchAsync().ConfigureAwait(false), () => CanSearch);
public ICommand SelectItemCommand
=> new Command<Prediction>(async (prediction) =>
{
//!!!Filter jobposts based on position and radius to be able to display it!!!
_api.ResetSessionToken();
});
private string _searchText;
public string SearchText
{
get => _searchText;
set
{
if (SetProperty(ref _searchText, value))
OnPropertyChanged("CanSearch");
}
}
private List<Prediction> _results;
public List<Prediction> Results
{
get => _results;
set => SetProperty(ref _results, value);
}
public bool CanSearch => !string.IsNullOrWhiteSpace(SearchText) && SearchText.Length > 2;
private async Task DoSearchAsync()
{
var results = await _api.GetPredictionsAsync(SearchText)
.ConfigureAwait(false);
if(results != null && results.Status.Equals("OK"))
{
ResultCount = results.Items.Count;
Results = results.Items;
OnPropertyChanged("HasResults");
}
}
}
public class JobPost
{
public Position Location {get;set;}
}
好的,到目前为止效果很好。单击搜索按钮后,求职者将获得一份预测列表并选择一个地方。但现在的问题是,我如何获得所选预测的定位(经度和纬度),以便我可以过滤职位,以便将结果显示给求职者。 提前致谢。
使用 Essentials GeoCoding 插件
var address = "New York";
var locations = await Geocoding.GetLocationsAsync(address);
locations
将是一个潜在匹配的列表,每个都有一个 lat/long
var location = locations?.FirstOrDefault();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}