문제

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'로 암시 적으로 변환 할 수 없습니다. 명시 적 변환이 존재합니다 (캐스트가 누락 되었습니까?) (CS0266)."

도움이 되었습니까?

해결책

이것은 실패한 선입니다.

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