Search This Blog

Sunday, 25 December 2016

Android ReverseGeocode Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Runtime.CompilerServices;
using Mig.Droid;
using Xamarin.Forms;
using Android.Locations;
using System.Threading.Tasks;

[assembly: Xamarin.Forms.Dependency(typeof(ReverseGeocode))]
namespace Mig.Droid
{
    public class ReverseGeocode : IReverseGeocode
    {
        public ReverseGeocode()
        {

        }
        public async Task<LocationAddress> ReverseGeoCodeLatLonAsync(double lat, double lon)
        {
            var geo = new Geocoder(Forms.Context);
            var addresses = await geo.GetFromLocationAsync(lat, lon, 1);
            if (addresses.Any())
            {
                var place = new LocationAddress();
                var add = addresses[0];
                place.Name = add.FeatureName;
                if (add.MaxAddressLineIndex > 0)
                {
                    place.Address1 = add.GetAddressLine(0);
                }
                place.City = add.Locality;
                place.Province = add.AdminArea;
                place.ZipCode = add.PostalCode;
                place.Country = add.CountryCode;
                return place;
            }

            return null;
        }
    }
}

No comments:

Post a Comment