Cross-Platform Development With Mono for Android: Listing 1

Geolocation with Mono for Android.

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using System.Threading;
using Xamarin.Geolocation;
 
namespace GeolocationSample
{
	[Activity (Label = "GeolocationSample", MainLauncher = true)]
	public class MainActivity : Activity
	{
		private Button toggleListenButton, cancelPositionButton;
		
		private TextView	positionStatus, positionLatitude, positionLongitude, positionAccuracy,
							listenStatus, listenLatitude, listenLongitude, listenAccuracy;
		
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);
 
			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);
 
			FindViewById<Button> (Resource.Id.getPositionButton)
				.Click += OnGetPosition;
			
			this.cancelPositionButton = FindViewById<Button> (Resource.Id.cancelPositionButton);
			this.cancelPositionButton.Click += OnCancelPosition;
			
			this.toggleListenButton = FindViewById<Button> (Resource.Id.toggleListeningButton);
			this.toggleListenButton.Click += OnToggleListening;
			
			this.positionStatus = FindViewById<TextView> (Resource.Id.status);
			this.positionAccuracy = FindViewById<TextView> (Resource.Id.pAccuracy);
			this.positionLatitude = FindViewById<TextView> (Resource.Id.pLatitude);
			this.positionLongitude = FindViewById<TextView> (Resource.Id.pLongitude);
			
			this.listenStatus = FindViewById<TextView> (Resource.Id.listenStatus);
			this.listenAccuracy = FindViewById<TextView> (Resource.Id.lAccuracy);
			this.listenLatitude = FindViewById<TextView> (Resource.Id.lLatitude);
			this.listenLongitude = FindViewById<TextView> (Resource.Id.lLongitude);
		}
		
		private Geolocator geolocator;
		private CancellationTokenSource cancelSource;
		
		private void Setup()
		{
			if (this.geolocator != null)
				return;
 
			this.geolocator = new Geolocator (this) { DesiredAccuracy = 50 };
			this.geolocator.PositionError += OnListeningError;
			this.geolocator.PositionChanged += OnPositionChanged;
		}
 
		private void OnGetPosition (object sender, EventArgs e)
		{
			Setup();
			
			this.cancelSource = new CancellationTokenSource();
			
			this.positionStatus.Text = String.Empty;
			this.positionAccuracy.Text = String.Empty;
			this.positionLatitude.Text = String.Empty;
			this.positionLongitude.Text = String.Empty;
			
			this.geolocator.GetPositionAsync (timeout: 10000, cancelToken: this.cancelSource.Token)
				.ContinueWith (t => RunOnUiThread (() =>
				{
					if (t.IsFaulted)
						this.positionStatus.Text = ((GeolocationException)t.
                                                Exception.InnerException).Error.ToString();
					else if (t.IsCanceled)
						this.positionStatus.Text = "Canceled";
					else
					{
						this.positionStatus.Text = t.Result.Timestamp.ToString("G");
						this.positionAccuracy.Text = t.Result.Accuracy + "m";
						this.positionLatitude.Text = "La: " + t.Result.Latitude.ToString("N4");
						this.positionLongitude.Text = "Lo: " + t.Result.Longitude.ToString("N4");
					}
				}));
		}
		
		private void OnCancelPosition (object sender, EventArgs e)
		{
			CancellationTokenSource cancel = this.cancelSource;
			if (cancel != null)
				cancel.Cancel();
		}
		
		private void OnToggleListening (object sender, EventArgs e)
		{
			Setup();
			
			if (!this.geolocator.IsListening)
			{
				this.toggleListenButton.SetText (Resource.String.stopListening);
				this.geolocator.StartListening (minTime: 30000, minDistance: 0);
			}
			else
			{
				this.toggleListenButton.SetText (Resource.String.startListening);
				this.geolocator.StopListening();
			}
		}
		
		private void OnListeningError (object sender, PositionErrorEventArgs e)
		{
			RunOnUiThread (() => {
				this.listenStatus.Text = e.Error.ToString();
			});
		}
		
		private void OnPositionChanged (object sender, PositionEventArgs e)
		{
			RunOnUiThread (() => {
				this.listenStatus.Text = e.Position.Timestamp.ToString("G");
				this.listenAccuracy.Text = e.Position.Accuracy + "m";
				this.listenLatitude.Text = "La: " + e.Position.Latitude.ToString("N4");
				this.listenLongitude.Text = "Lo: " + e.Position.Longitude.ToString("N4");
			});
		}
	}
}

About the Author

Wallace (Wally) B. McClure has authored books on iPhone programming with Mono/Monotouch, Android programming with Mono for Android, application architecture, ADO.NET, SQL Server and AJAX. He's a Microsoft MVP, an ASPInsider and a partner at Scalable Development Inc. He maintains a blog, and can be followed on Twitter.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube