如何实施 google 将 api 放入 Xamarin.Android

How to implement google places api into Xamarin.Android

我正在我的应用程序上应用 Google Places Api 并将 Java 转换为 C# 代码。但是我得到了这个错误。

提前致谢。

Cannot implicitly convert type 'Java.Lang.Object' to 'Android.Runtime.JavaDictionary'. An explicit conversion exists (are you missing a cast?)

我注意到在您的 中,placeList 的类型是 JavaList, 你应该注意到 JavaList.Get(position) return 是 Java.Lang.Object 的类型。您不能直接将其转换为 JavaDictionary.

JavaList Get(int location) 方法:

public class JavaList : Java.Lang.Object, System.Collections.IList, System.Collections.ICollection, IEnumerable
{
    ...
    public virtual Java.Lang.Object Get(int location);
}

解决方案 1:

正如@Jon Douglas 所说,您可以使用 JavaCast :

JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace).JavaCast<JavaDictionary<string, string>>();

解决方案 2:

你可以使用 System.Linq :

JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);

完整代码:

using System.Linq;
...
private void showNearbyPlaces(JavaList<JavaDictionary<string, string>> placeList)
{
    for (int eachPlace = 0; eachPlace < placeList.Count; eachPlace++)
    {       
        JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);

        string reference = googlePlace["reference"];
        double lat = System.Double.Parse(googlePlace["lat"]);
    }
}