如何获得GPS或位置

时间:2013-05-16 03:11:58

标签: xamarin.android

我尝试了GPS教程,但收到以下错误消息。什么似乎是问题?

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

如何解决这些问题:

错误消息

1)

  

错误CS1061:'System.Collections.Generic.IList'没有   包含'Any'的定义,没有扩展方法'Any'接受   “System.Collections.Generic.IList”类型的第一个参数   可以找到(你错过了使用指令或程序集   参考?)(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)如果文件名是MainActivity.cs并且调用了Activity Class,那么它是否重要:

public class Activity1 : Activity, ILocationListener
{
}

由于

1 个答案:

答案 0 :(得分:1)

使用&#34; Any()&#34;的定义你必须使用System.Linq&#34;。

希望有所帮助。