Этот код не работает, а ошибка отображается при компиляции?

StackOverflow https://stackoverflow.com/questions/19855681

Вопрос

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);

            }
        }
   }
}

Сообщение об ошибке: он показывает ошибку «не может быть неявно преобразовать тип 'java.util.ilist' в Android.location.address '.

Это было полезно?

Решение

Это линия, которая терпит неудачу:

adds = geo.GetFromLocation(location.GetLatitude(), location.GetLongitude(), 1)

geo.GetFromLocation возврат Java.Util.IList<Address>. adds имеет тип Address. Анкет Отсюда ошибка компиляции.

Используйте оператор индекса для доступа к одному из адресов.

enter image description here

РЕДАКТИРОВАТЬ

Кроме того, вы должны инициализировать geo Перед его использованием:

Geocoder geo = new Geocoder(this, Locale.getDefault()); 

Наконец, GetFromLocation может вернуть NULL или пустой список, поэтому проверьте оба.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top