1

I tried the tutorial GPS and I got the following error messages. What seems to be the problems?

http://docs.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

//-- added  these


using Android.Locations;
using System.Collections.Generic;
using System.Threading;
using System.Text;


namespace GetLocation
{

    [Activity(Label = "Get Location", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity, ILocationListener
    {
        //int count = 1;
        private Location _currentLocation;
        private LocationManager _locationManager;
        private TextView _locationText;
        private TextView _addressText;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView(Resource.Layout.Main);
            _addressText = FindViewById(Resource.Id.address_text);
            _locationText = FindViewById(Resource.Id.location_text);
            FindViewById(Resource.Id.get_address_button).Click += AddressButton_OnClick;

            InitializeLocationManager();        
        }

        //public void OnLocationChanged(Location location) {}
        public void OnProviderDisabled(string provider) {}
        public void OnProviderEnabled(string provider) {}
        public void OnStatusChanged(string provider, Availability status, Bundle extras) {}

        private void InitializeLocationManager()
        {
            _locationManager = (LocationManager) GetSystemService(LocationService);
            var criteriaForLocationService = new Criteria
            {
                Accuracy = Accuracy.Fine
            };
            var acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

            if (acceptableLocationProviders.Any())
            {
                _locationProvider = acceptableLocationProviders.First();
            }
            else
            {
                _locationProvider = String.Empty;
            }
        }

        protected override void OnResume()
        {
            base.OnResume();
            _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
        }

        protected override void OnPause()
        {
            base.OnPause();
            _locationManager.RemoveUpdates(this);
        }

        private void AddressButton_OnClick(object sender, EventArgs eventArgs)
        {
            if (_currentLocation == null)
            {
                _addressText.Text = "Can't determine the current location.";
                return;
            }
            new Thread(() =>
                       {
                var addressText = "Unable to find a location.";
                var geocoder = new Geocoder(this);
                var addressList = geocoder.GetFromLocation(_currentLocation.Latitude, _currentLocation.Longitude, 50);
                var address = addressList.FirstOrDefault();

                if (address != null)
                {
                    var deviceLocation = new StringBuilder();
                    for (var i = 0; i  { _addressText.Text = addressText; });
            }).Start();
        }

        public void OnLocationChanged(Location location)
        {
            _currentLocation = location;
            if (_currentLocation == null)
            {
                _locationText.Text = "Unable to determine your location.";
            }
            else
            {
                _locationText.Text = String.Format("{0},{1}", _currentLocation.Latitude, _currentLocation.Longitude);
            }
        }
    }
}

How to solve these problems:

Error messages

1)

Error CS1061: 'System.Collections.Generic.IList' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'System.Collections.Generic.IList' could be found (are you missing a using directive or an assembly reference?) (CS1061) (GetLocation)

2)Error CS0103: The name '_locationProvider' does not exist in the current context (CS0103) (GetLocation)

3)Error CS1061: 'System.Collections.Generic.IList<string>' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'System.Collections.Generic.IList<string>' could be found (are you missing a using directive or an assembly reference?) (CS1061) (GetLocation)

4)Error CS0103: The name '_locationProvider' does not exist in the current context (CS0103) (GetLocation)

5) Error CS1061: 'System.Collections.Generic.IList<Android.Locations.Address>' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Collections.Generic.IList<Android.Locations.Address>' could be found (are you missing a using directive or an assembly reference?) (CS1061) (GetLocation)

6) Does it matter if File name is MainActivity.cs and Activity Class is called:

public class Activity1 : Activity, ILocationListener
{
}

Thanks

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
MilkBottle
  • 4,242
  • 13
  • 64
  • 146
  • Have you tried this tutorial? It seems different from your code so maybe this will work better. http://docs.xamarin.com/guides/android/platform_features/maps_and_location/part_3_-_location_services – jHogen May 16 '13 at 07:20
  • @jHogen: Thanks for the link. I have added system.linq, system.xml and declare a private string _locationProvider ( this is missing ) . Now it has no compilation error but NO Gps , it says Cannot determine the location. I am testing on emulator, can it provide a default Gps? – MilkBottle May 16 '13 at 08:03
  • Take a look at this question: http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator – jHogen May 16 '13 at 08:29
  • Thanks. Will look at it and report back – MilkBottle May 16 '13 at 09:09
  • @jHogen: The link is for Eclipse, How to do it in Xamarin Studio? – MilkBottle May 19 '13 at 01:49
  • Well as far as I can see the solution has nothing to do with eclipse. It's just a explanation for the android emulator, which is the same as you use. What exactly doesn't work for you in that link? – jHogen May 21 '13 at 07:13

1 Answers1

1

to use the definition of "Any()" you must have "using System.Linq".

Hope that helps.