If you are using Xamarin.Forms maybe this code can be usefull:
using System;
using System.Globalization;
using Xamarin.Forms;
namespace GeonetPost.Xamarin.Converters
{
public class GeographicCoordinateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.GetType() == typeof(double) && targetType == typeof(string))
{
return ToDMS((double)value, (string)parameter);
}
return value;
}
private string ToDMS(double valor, string eje)
{
var cSigno = string.Empty;
var signo = Math.Sign(valor);
valor = Math.Abs(valor);
if (!string.IsNullOrEmpty(eje))
{
cSigno = eje.ToUpper() == "X" ? signo > 0 ? "E" : "W" : signo > 0 ? "N" : "S";
signo = 1;
}
var grados = Math.Floor(valor);
var minutos = Math.Floor((valor - grados) * 60);
var segundos = ((valor - grados) * 60 - minutos) * 60;
return string.Format("{0:0}°{1:00}'{2:00.000}\" {3}", signo * grados, minutos, segundos, cSigno);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
In XAML code you can use the converter like in the next code:
<ContentPage.Resources>
<ResourceDictionary>
<cv:GeographicCoordinateConverter x:Key="GeoCoorConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Orientation="Vertical"
Margin="10"
HeightRequest="100">
<Label Text="{Binding UpdatedViewpoint.TargetGeometry.XMin,
Converter={StaticResource GeoCoorConverter},
ConverterParameter='X',
StringFormat='XMin = {0}'}" />
<Label Text="{Binding UpdatedViewpoint.TargetGeometry.YMin,
Converter={StaticResource GeoCoorConverter},
ConverterParameter='Y',
StringFormat='YMin = {0}'}" />
<Label Text="{Binding UpdatedViewpoint.TargetGeometry.XMax,
Converter={StaticResource GeoCoorConverter},
ConverterParameter='X',
StringFormat='XMax = {0}'}" />
<Label Text="{Binding UpdatedViewpoint.TargetGeometry.YMax,
Converter={StaticResource GeoCoorConverter},
ConverterParameter='Y',
StringFormat='YMax = {0}'}" />
</StackLayout>
To see the complete sample code visit:
https://github.com/marceloctorres/GeonetPost.Xamarin (Xamarin sample) or https://github.com/marceloctorres/GeonetPost.WPF (WPF sample).
Marcelo César Torres