Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active November 25, 2019 11:37
Show Gist options
  • Save icebeam7/23793edf49ab229f40263f3860e847d9 to your computer and use it in GitHub Desktop.
Save icebeam7/23793edf49ab229f40263f3860e847d9 to your computer and use it in GitHub Desktop.
DemoDS: ReverseGeocodeAndroid.cs
using System.Linq;
using System.Threading.Tasks;
using Android.Locations;
using Xamarin.Forms;
using DemoDS.Models;
using DemoDS.Interfaces;
[assembly: Dependency(typeof(DemoDS.Droid.Dependency.ReverseGeocodeAndroid))]
namespace DemoDS.Droid.Dependency
{
public class ReverseGeocodeAndroid : IReverseGeocode
{
public async Task<LocationAddress> GetLocationAddress(double latitude, double longitude)
{
var geoCoder = new Geocoder(Android.App.Application.Context);
var addresses = await geoCoder.GetFromLocationAsync(latitude, longitude, 1);
if (addresses.Any())
{
var address = addresses.First();
var location = new LocationAddress()
{
Name = address.FeatureName,
City = address.Locality,
Province = address.AdminArea,
ZipCode = address.PostalCode,
Country = $"{address.CountryName} ({address.CountryCode})",
Address = address.MaxAddressLineIndex > 0 ? address.GetAddressLine(0) : string.Empty
};
return location;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment