我試過教程GPS,我收到以下錯誤消息。什麼似乎是問題?如何獲取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和活動課叫做:
public class Activity1 : Activity, ILocationListener
{
}
感謝
您是否試過本教程?它看起來不同於你的代碼,所以也許這會更好。 http://docs.xamarin.com/guides/android/platform_features/maps_and_location/part_3_-_location_services – jHogen
@jHogen:感謝您的鏈接。我添加了system.linq,system.xml並聲明瞭一個私有字符串_locationProvider(這是缺少的)。現在它沒有編譯錯誤,但沒有Gps,它說無法確定位置。我在模擬器上測試,可以提供默認的Gps嗎? – MilkBottle
看看這個問題:http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator – jHogen