Last active
November 25, 2019 11:37
-
-
Save icebeam7/23793edf49ab229f40263f3860e847d9 to your computer and use it in GitHub Desktop.
DemoDS: ReverseGeocodeAndroid.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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