Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Greg Shackles January 23, 2011
file 83 lines (68 sloc) 2.647 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
using System.Linq;
using System.Text;
using Android.App;
using Android.Locations;
using Android.OS;
using Android.Widget;
using Java.IO;

namespace MonoDroidSamples.DemoActivities.LocationDemo
{
[Activity(Label = "Location Demo")]
    public class LocationActivity : Activity, ILocationListener
    {
        private TextView _locationText;
        private LocationManager _locationManager;
        private StringBuilder _builder;
        private Geocoder _geocoder;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.location_demo);

            _builder = new StringBuilder();
            _geocoder = new Geocoder(this);
            _locationText = FindViewById<TextView>(Resource.Id.location_text);
            _locationManager = (LocationManager)GetSystemService(LocationService);

            var criteria = new Criteria() { Accuracy = Accuracy.NoRequirement };
            string bestProvider = _locationManager.GetBestProvider(criteria, true);

            Location lastKnownLocation = _locationManager.GetLastKnownLocation(bestProvider);

            if (lastKnownLocation != null)
            {
                _locationText.Text = string.Format("Last known location, lat: {0}, long: {1}",
                                                   lastKnownLocation.Latitude, lastKnownLocation.Longitude);
            }

            _locationManager.RequestLocationUpdates(bestProvider, 5000, 2, this);
        }

        public void OnLocationChanged(Location location)
        {
            _builder.AppendLine(
                string.Format("Location updated, lat: {0}, long: {1}",
                              location.Latitude, location.Longitude)
            );
            
            try
            {
                Address address =
                    _geocoder
                        .GetFromLocation(location.Latitude, location.Longitude, 1)
                        .FirstOrDefault();

                if (address != null)
                {
                    _builder.AppendLine(" -> " + address.CountryName);
                }
            }
            catch (IOException io)
            {
                Android.Util.Log.Debug("LocationActivity", io.Message);
            }

            _locationText.Text = _builder.ToString();
        }

        public void OnProviderDisabled(string provider)
        {
        }

        public void OnProviderEnabled(string provider)
        {
        }

        public void OnStatusChanged(string provider, int status, Bundle extras)
        {
        }
    }
}
Something went wrong with that request. Please try again.