Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active November 25, 2019 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/871ad4bd5d103eaef00be962c3f2188f to your computer and use it in GitHub Desktop.
Save icebeam7/871ad4bd5d103eaef00be962c3f2188f to your computer and use it in GitHub Desktop.
DemoDS: ReverseGeocodeiOS.cs
using System.Linq;
using System.Threading.Tasks;
using Contacts;
using CoreLocation;
using DemoDS.Models;
using DemoDS.Interfaces;
[assembly: Xamarin.Forms.Dependency(typeof(DemoDS.iOS.Dependency.ReverseGeocodeiOS))]
namespace DemoDS.iOS.Dependency
{
public class ReverseGeocodeiOS : IReverseGeocode
{
public async Task<LocationAddress>GetLocationAddress(double latitude, double longitude)
{
var geoCoder = new CLGeocoder();
var place = new CLLocation(latitude, longitude);
var placemarks = await geoCoder.ReverseGeocodeLocationAsync(place);
if (placemarks.Any())
{
var placeMark = placemarks.First();
var location = new LocationAddress()
{
Name = placeMark.Name,
City = placeMark.Locality,
Province = placeMark.AdministrativeArea,
ZipCode = placeMark.PostalCode,
Country = $"{placeMark.Country} ({placeMark.IsoCountryCode})",
Address = new CNPostalAddressFormatter().StringFor(placeMark.PostalAddress)
};
return location;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment