Geolocation 此代码无效,编译时显示错误?
错误消息:Geolocation 此代码无效,编译时显示错误?,geolocation,street-address,google-geocoder,dot42,Geolocation,Street Address,Google Geocoder,Dot42,错误消息: 它显示错误无法将类型“Java.Util.IList”隐式转换为“Android.Location.Address”。存在显式转换。是否缺少强制转换?CS0266这是失败的线路: using System; using Android.App; using Android.Os; using Android.Widget; using Dot42; using Dot42.Manifest; using Android.Location; [assembly:UsesPermis
它显示错误无法将类型“Java.Util.IList”隐式转换为“Android.Location.Address”。存在显式转换。是否缺少强制转换?CS0266这是失败的线路:
using System;
using Android.App;
using Android.Os;
using Android.Widget;
using Dot42;
using Dot42.Manifest;
using Android.Location;
[assembly:UsesPermission(Android.Manifest.Permission.ACCESS_COARSE_LOCATION)]
[assembly:UsesPermission(Android.Manifest.Permission.INTERNET)]
[assembly:UsesPermission(Android.Manifest.Permission.ACCESS_FINE_LOCATION)]
[assembly: Application("simplegps")]
namespace simplegps
{
[Activity]
public class MainActivity : Activity
{
private LocationManager service;
private bool enable;
private string provider;
protected override void OnCreate(Bundle savedInstance)
{
base.OnCreate(savedInstance);
SetContentView(R.Layouts.MainLayout);
var txtprovider= FindViewById <TextView>(R.Ids.txtprovider);
var gpsstatus= FindViewById <TextView>(R.Ids.gpsstatus);
var txtcity = FindViewById<TextView>(R.Ids.txtcity);
var txtlat = FindViewById<TextView>(R.Ids.txtlat);
var txtlon = FindViewById<TextView>(R.Ids.txtlon);
service=(LocationManager)GetSystemService(LOCATION_SERVICE);
enable=service.IsProviderEnabled(LocationManager.GPS_PROVIDER);
if(enable)
{
gpsstatus.Text="Gps enabled";
}
else
{
gpsstatus.Text="Gps not enabled";
return;
}
var criteria = new Criteria{Accuracy = Criteria.ACCURACY_FINE};
provider = service.GetBestProvider(criteria,false);
var location = service.GetLastKnownLocation(provider);
if(location !=null)
{
txtprovider.Text=provider;
var latitude = location.Latitude;
var longitude = location.Longitude;
txtlat.Text=latitude.ToString();
txtlon.Text=longitude.ToString();
}
else
{
txtprovider.Text="no location";
return;
}
if(Geocoder.IsPresent())
{
Android.Location.Geocoder geo;
Android.Location.Address adds;
adds=geo.GetFromLocation(location.GetLatitude(),location.GetLongitude(),1);
}
}
}
}
geo.GetFromLocation返回Java.Util.IList。adds的类型为Address。因此出现了编译错误
使用索引运算符访问其中一个地址
编辑
此外,在使用geo之前,您应该初始化它:
adds = geo.GetFromLocation(location.GetLatitude(), location.GetLongitude(), 1)
最后,GetFromLocation可能返回null或空列表,因此请检查两者。您正试图将一种类型的列表强制转换为一种类型的地址。但不知道在哪里。它可能在dot42代码中的某个地方。您应该将其报告为bug,并尝试使用本机JAVA方式。
Geocoder geo = new Geocoder(this, Locale.getDefault());