Hi,
I tried the tutorial GPS from this link: 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' does not contain a definition for 'First' and no extension method 'First' 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)
4)Error CS0103: The name '_locationProvider' does not exist in the current context (CS0103) (GetLocation)
5) Error CS1061: 'System.Collections.Generic.IList' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' 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)
6) Does it matter if File name is MainActivity.cs and Activity Class is called:
public class Activity1 : Activity, ILocationListener
For problem 1 to 5 , I think I need to add DLL for System.Core , system.XML, How to I add these in Xamarin Studio ?
Thanks