我正在尝试将自动完成服务添加到我的 Xamarin 表单映射的 Xamarin 表单上的自定义搜索栏(我正在使用条目(,但我无法这样做。
我已经尝试过使用 XLabs,但没有奏效,到目前为止我还没有找到任何其他解决方案。
Xamarin.FormsMaps 是否包含任何自动完成功能?或者 Xamarin 上是否已经有任何我可能缺少的自动完成功能? 你对我有什么建议?如果您需要任何代码或其他任何内容,请告诉我。
这是一个很长的问题,但在这里....显然,您希望将服务调用代码放在服务class
中,并将所有其他代码放在ViewModel
中。您可能还希望在服务调用方法中或周围添加更多 null 和错误检查。
我省略的一个重要部分是谷歌的要求,即在执行搜索时显示谷歌标志。因此,下载他们的图标并将其添加到UI中。我只在用户专注于Entry
时才显示它。
所以这些是地方模型和我自己的Address
模型:
public class AddressInfo {
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
}
public class PlacesMatchedSubstring {
[Newtonsoft.Json.JsonProperty("length")]
public int Length { get; set; }
[Newtonsoft.Json.JsonProperty("offset")]
public int Offset { get; set; }
}
public class PlacesTerm {
[Newtonsoft.Json.JsonProperty("offset")]
public int Offset { get; set; }
[Newtonsoft.Json.JsonProperty("value")]
public string Value { get; set; }
}
public class Prediction {
[Newtonsoft.Json.JsonProperty("id")]
public string Id { get; set; }
[Newtonsoft.Json.JsonProperty("description")]
public string Description { get; set; }
[Newtonsoft.Json.JsonProperty("matched_substrings")]
public List<PlacesMatchedSubstring> MatchedSubstrings { get; set; }
[Newtonsoft.Json.JsonProperty("place_id")]
public string PlaceId { get; set; }
[Newtonsoft.Json.JsonProperty("reference")]
public string Reference { get; set; }
[Newtonsoft.Json.JsonProperty("terms")]
public List<PlacesTerm> Terms { get; set; }
[Newtonsoft.Json.JsonProperty("types")]
public List<string> Types { get; set; }
}
public class PlacesLocationPredictions {
[Newtonsoft.Json.JsonProperty("predictions")]
public List<Prediction> Predictions { get; set; }
[Newtonsoft.Json.JsonProperty("status")]
public string Status { get; set; }
}
这里我们称之为 Places API:
public const string GooglePlacesApiAutoCompletePath = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key={0}&input={1}&components=country:us"; //Adding country:us limits results to us
public const string GooglePlacesApiKey = "bTafrOPmO4LpPgAl34r5wQ6LFRWhgTxBW80-3GK";
private static HttpClient _httpClientInstance;
public static HttpClient HttpClientInstance => _httpClientInstance ?? (_httpClientInstance = new HttpClient());
private ObservableCollection<AddressInfo> _addresses;
public ObservableCollection<AddressInfo> Addresses {
get => _addresses ?? (_addresses = new ObservableCollection<AddressInfo>());
set {
if(_addresses != value) {
_addresses = value;
OnPropertyChanged();
}
};
}
private string _addressText;
public string AddressText {
get => _addressText;
set {
if(_addressText != value) {
_addressText = value;
OnPropertyChanged();
}
};
}
public async Task GetPlacesPredictionsAsync() {
// TODO: Add throttle logic, Google begins denying requests if too many are made in a short amount of time
CancellationToken cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(2)).Token;
using(HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format(GooglePlacesApiAutoCompletePath, ConstantKeys.GooglePlacesApiKey, WebUtility.UrlEncode(_addressText)))) { //Be sure to UrlEncode the search term they enter
using(HttpResponseMessage message = await HttpClientInstance.SendAsync(request, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false)) {
if(message.IsSuccessStatusCode) {
string json = await message.Content.ReadAsStringAsync().ConfigureAwait(false);
PlacesLocationPredictions predictionList = await Task.Run(() => JsonConvert.DeserializeObject<PlacesLocationPredictions>(json)).ConfigureAwait(false);
if(predictionList.Status == "OK") {
Addresses.Clear();
if(predictionList.Predictions.Count > 0) {
foreach(Prediction prediction in predictionList.Predictions) {
Addresses.Add(new AddressInfo {
Address = prediction.Description
});
}
}
} else {
throw new Exception(predictionList.Status);
}
}
}
}
}
现在对于用户界面...
<Entry Text="{Binding AddressText}"
TextChanged="OnTextChanged" />
<ListView ItemsSource="{Binding Addresses}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Address}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
和代码隐藏:
private async void OnTextChanged(object sender, EventArgs eventArgs) {
if(!string.IsNullOrWhiteSpace(ViewModel.AddressText)) {
await ViewModel.GetPlacesPredictionsAsync();
}
}