【问题标题】:How to get GPS or Location如何获取 GPS 或位置信息
【发布时间】:2013-05-10 19:30:07
【问题描述】:

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

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

使用系统; 使用Android.App; 使用Android.Content; 使用 Android.Runtime; 使用 Android.Views; 使用 Android.Widget; 使用Android.OS; //--- 添加了这些 使用 Android.Locations; 使用 System.Collections.Generic; 使用 System.Threading; 使用 System.Text; 命名空间 GetLocation { [活动(标签=“获取位置”,MainLauncher = true,图标=“@dr​​awable/icon”)] 公共类 Activity1:活动,ILocationListener { //int 计数 = 1; 私人位置_currentLocation; 私人位置管理器_位置管理器; 私有TextView _locationText; 私有TextView _addressText; protected override void OnCreate(捆绑包) { base.OnCreate(捆绑); 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; 初始化位置管理器(); } //public void OnLocationChanged(位置位置) {} 公共无效 OnProviderDisabled(字符串提供者){} 公共无效 OnProviderEnabled(字符串提供者){} public void OnStatusChanged(string provider, Availability status, Bundle extras) {} 私人无效 InitializeLocationManager() { _locationManager = (LocationManager) GetSystemService(LocationService); var criteriaForLocationService = 新标准 { 准确度 = 准确度. 精细 }; var 可接受的LocationProviders = _locationManager.GetProviders(criteriaForLocationService, true); if (acceptableLocationProviders.Any()) { _locationProvider = 可接受的LocationProviders.First(); } 别的 { _locationProvider = String.Empty; } } 受保护的覆盖无效 OnResume() { base.OnResume(); _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this); } 受保护的覆盖无效 OnPause() { base.OnPause(); _locationManager.RemoveUpdates(this); } private void AddressButton_OnClick(对象发送者,EventArgs eventArgs) { 如果(_currentLocation == null) { _addressText.Text = "无法确定当前位置。"; 返回; } 新线程(()=> { var addressText = "找不到位置。"; var geocoder = new Geocoder(this); var addressList = geocoder.GetFromLocation(_currentLocation.Latitude, _currentLocation.Longitude, 50); var address = addressList.FirstOrDefault(); 如果(地址!= null) { var deviceLocation = new StringBuilder(); for (var i = 0; i { _addressText.Text = addressText; }); })。开始(); } public void OnLocationChanged(位置位置) { _currentLocation = 位置; 如果(_currentLocation == null) { _locationText.Text = "无法确定您的位置。"; } 别的 { _locationText.Text = String.Format("{0},{1}", _currentLocation.Latitude, _currentLocation.Longitude); } } } }

如何解决这些问题:

错误信息

1)

错误 CS1061:'System.Collections.Generic.IList' 没有 包含 'Any' 的定义并且没有扩展方法 'Any' 接受 'System.Collections.Generic.IList' 类型的第一个参数 可以找到(您是否缺少 using 指令或程序集 参考?)(CS1061)(获取位置)

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 类是否重要:

public class Activity1 : Activity, ILocationListener
{
}

谢谢

【问题讨论】:

  • 你试过这个教程吗?它似乎与您的代码不同,所以也许这会更好。 docs.xamarin.com/guides/android/platform_features/…
  • @jHogen:感谢您的链接。我添加了 system.linq、system.xml 并声明了一个私有字符串 _locationProvider(这是缺失的)。现在它没有编译错误但没有 Gps ,它说无法确定位置。我正在模拟器上测试,它可以提供默认的 Gps 吗?
  • 谢谢。会查看并报告
  • @jHogen:该链接适用于 Eclipse,如何在 Xamarin Studio 中进行操作?

标签: xamarin.android


【解决方案1】:

要使用“Any()”的定义,您必须具有“使用 System.Linq”。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 2016-05-16
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多