Xamarin Android Google 地方 Api 自动完成损坏
Xamarin Android Google Places Api AutoComplete broken
我正在尝试使用 Xamarin Android.
从 Google's Places Api 获得基本的自动完成功能
我使用的是 Xamarin Google Play Services - Location 库的 25.0.0.0 版。
时,我已经设法返回查询结果
这是我用来测试代码的片段
public class PlaceAutocomplete: BaseFragment, IGoogleApiClientOnConnectionFailedListener {
IGoogleApiClient client;
public override void OnCreate( Bundle savedInstanceState ) {
base.OnCreate( savedInstanceState );
client = new GoogleApiClientBuilder( Activity )
.EnableAutoManage( Activity as BaseFragmentActivity, 0, this )
.AddOnConnectionFailedListener( OnConnectionFailed )
.AddApi( Places.GEO_DATA_API )
.Build();
}
public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
Places.GeoDataApi.GetAutocompletePredictions( client, "cul",
new LatLngBounds( new LatLng( 33.515071, -118.796427 ), new LatLng( 34.435985, -117.127371 ) ), null )
.SetResultCallback<AutocompletePredictionBuffer>( AutocompleteResult );
return base.OnCreateView( inflater, container, savedInstanceState );
}
public void AutocompleteResult( AutocompletePredictionBuffer buffer ) {
if( !buffer.Status.IsSuccess ) {
Toast.MakeText( Activity, buffer.Status.StatusMessage, ToastLength.Long ).Show();
return;
}
var a = new List<IAutocompletePrediction>();
for( var i = 0; i < buffer.Count; i++ ) {
var item = buffer.Get( i );
if( item is IAutocompletePrediction ) {
a.Add( (IAutocompletePrediction) item );
} else {
//all the results go in here
}
}
Toast.MakeText( Activity, a.Count.ToString(), ToastLength.Long ).Show();
}
public void OnConnectionFailed( ConnectionResult result ) {
}
}
GetAutocompletePredictions 方法返回了 5 个结果,但它们的类型均为 com.google.android.gms.location.places.internal.zzb,无法转换为IAutocompletePrediction 并且我没有找到任何使用它们的方法。
是我做错了什么还是 Xamarin 的 Google Play 库的这一部分没有完全实现?
编辑 - 新信息
这不是错误。 IAutocompletePrediction 当前未由 Xamarin 实现,因此您必须像这样转换它
item.JavaCast<IAutocompletePrediction>()
为了使用它。
旧信息
我已与 Xamarin 支持人员交谈,他们已确认这是一个错误。
可以在此处找到有关错误修复的更多信息https://bugzilla.xamarin.com/show_bug.cgi?id=31878
截至
Xamarin 4.0.0.1717 /
Xamarin.Android 6.0.0.35 /
Xamarin Google Play Services Location 组件 v 27.0.0.0
,看来这个 space 有一些工作要做。
IEnumerable 接口现在提供了一种获取类型化响应的方法,下面的代码片段(替换了 AutocompleteResult 的一部分)有效:
var a = new List<IAutocompletePrediction>();
String t = "";
foreach (IAutocompletePrediction x in buffer)
{
a.Add(x);
t += "\n" + x.GetFullTextFormatted(new StyleSpan(TypefaceStyle.Normal)).ToString();
}
Toast.MakeText(Activity, t /* a.Count.ToString() */, ToastLength.Long).Show();
我正在尝试使用 Xamarin Android.
从 Google's Places Api 获得基本的自动完成功能我使用的是 Xamarin Google Play Services - Location 库的 25.0.0.0 版。
时,我已经设法返回查询结果这是我用来测试代码的片段
public class PlaceAutocomplete: BaseFragment, IGoogleApiClientOnConnectionFailedListener {
IGoogleApiClient client;
public override void OnCreate( Bundle savedInstanceState ) {
base.OnCreate( savedInstanceState );
client = new GoogleApiClientBuilder( Activity )
.EnableAutoManage( Activity as BaseFragmentActivity, 0, this )
.AddOnConnectionFailedListener( OnConnectionFailed )
.AddApi( Places.GEO_DATA_API )
.Build();
}
public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
Places.GeoDataApi.GetAutocompletePredictions( client, "cul",
new LatLngBounds( new LatLng( 33.515071, -118.796427 ), new LatLng( 34.435985, -117.127371 ) ), null )
.SetResultCallback<AutocompletePredictionBuffer>( AutocompleteResult );
return base.OnCreateView( inflater, container, savedInstanceState );
}
public void AutocompleteResult( AutocompletePredictionBuffer buffer ) {
if( !buffer.Status.IsSuccess ) {
Toast.MakeText( Activity, buffer.Status.StatusMessage, ToastLength.Long ).Show();
return;
}
var a = new List<IAutocompletePrediction>();
for( var i = 0; i < buffer.Count; i++ ) {
var item = buffer.Get( i );
if( item is IAutocompletePrediction ) {
a.Add( (IAutocompletePrediction) item );
} else {
//all the results go in here
}
}
Toast.MakeText( Activity, a.Count.ToString(), ToastLength.Long ).Show();
}
public void OnConnectionFailed( ConnectionResult result ) {
}
}
GetAutocompletePredictions 方法返回了 5 个结果,但它们的类型均为 com.google.android.gms.location.places.internal.zzb,无法转换为IAutocompletePrediction 并且我没有找到任何使用它们的方法。
是我做错了什么还是 Xamarin 的 Google Play 库的这一部分没有完全实现?
编辑 - 新信息
这不是错误。 IAutocompletePrediction 当前未由 Xamarin 实现,因此您必须像这样转换它
item.JavaCast<IAutocompletePrediction>()
为了使用它。
旧信息
我已与 Xamarin 支持人员交谈,他们已确认这是一个错误。
可以在此处找到有关错误修复的更多信息https://bugzilla.xamarin.com/show_bug.cgi?id=31878
截至
Xamarin 4.0.0.1717 /
Xamarin.Android 6.0.0.35 /
Xamarin Google Play Services Location 组件 v 27.0.0.0
,看来这个 space 有一些工作要做。 IEnumerable 接口现在提供了一种获取类型化响应的方法,下面的代码片段(替换了 AutocompleteResult 的一部分)有效:
var a = new List<IAutocompletePrediction>();
String t = "";
foreach (IAutocompletePrediction x in buffer)
{
a.Add(x);
t += "\n" + x.GetFullTextFormatted(new StyleSpan(TypefaceStyle.Normal)).ToString();
}
Toast.MakeText(Activity, t /* a.Count.ToString() */, ToastLength.Long).Show();