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

            }
        }
   }
}

error message: It shows error "Cannot implicitly convert type 'Java.Util.IList' to 'Android.Location.Address'. An explicit conversion exists (are you missing a cast?) (CS0266)"

Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
desolator
  • 27
  • 5
  • You are trying to cast a a type of List to a type of Address. Not sure where though. It is possible that its somewhere in the dot42 code. You should report it as a bug and try to use the Native JAVA way instead. – Piotr Kula Nov 08 '13 at 16:17

1 Answers1

0

This is the line that fails:

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

geo.GetFromLocation returns Java.Util.IList<Address>. adds is of type Address. Hence the compile error.

Use the index operator to access one of the Addresses.

enter image description here

EDIT

Also, you should initialize geo before using it:

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

Finally, GetFromLocation may return null or an empty list, so check for both.

Frank Rem
  • 3,632
  • 2
  • 25
  • 37