Skip to content

Instantly share code, notes, and snippets.

@vmrocha
Last active December 17, 2015 19:09
Show Gist options
  • Save vmrocha/5658758 to your computer and use it in GitHub Desktop.
Save vmrocha/5658758 to your computer and use it in GitHub Desktop.
LocationHelper that and GPS DMS converter.
public static class LocationHelper {
public static async Task<Geoposition> GetCurrentPosition() {
Geolocator locator = new Geolocator();
return await locator.GetGeopositionAsync();
}
public static async Task SavePosition(StorageFile file, Geoposition position) {
DMS latDms = new DMS(position.Coordinate.Latitude, Coordenate.Latitude);
DMS lonDms = new DMS(position.Coordinate.Longitude, Coordenate.Longitude);
int[] latitude = new int[] { latDms.Degrees, latDms.Minutes, latDms.Seconds };
int[] longitude = new int[] { lonDms.Degrees, lonDms.Minutes, lonDms.Seconds };
int[] denominator = new int[3] { 1, 1, 1 };
List<KeyValuePair<string, object>> extraProperties = new List<KeyValuePair<string, object>>();
extraProperties.Add(new KeyValuePair<string, object>("System.GPS.LongitudeNumerator", longitude));
extraProperties.Add(new KeyValuePair<string, object>("System.GPS.LongitudeDenominator", denominator));
extraProperties.Add(new KeyValuePair<string, object>("System.GPS.LongitudeRef", latDms.Direction.ToString()));
extraProperties.Add(new KeyValuePair<string, object>("System.GPS.LatitudeNumerator", latitude));
extraProperties.Add(new KeyValuePair<string, object>("System.GPS.LatitudeDenominator", denominator));
extraProperties.Add(new KeyValuePair<string, object>("System.GPS.LatitudeRef", lonDms.Direction.ToString()));
await file.Properties.SavePropertiesAsync(extraProperties);
}
}
public enum Coordenate {
Latitude,
Longitude
}
public enum Direction {
N,
S,
E,
W
}
public class DMS {
public DMS(double value, Coordenate coordenate) {
this.SetValue(value, coordenate);
}
public double Value { get; private set; }
public int Seconds { get; private set; }
public int Degrees { get; private set; }
public int Minutes { get; private set; }
public Direction Direction { get; private set; }
private void SetValue(double value, Coordenate coordenate) {
this.Value = value;
if (coordenate == Coordenate.Latitude) {
if (value >= 0) {
this.Direction = Direction.N;
}
else {
this.Direction = Direction.S;
value = Math.Abs(value);
}
}
else {
if (value >= 0) {
this.Direction = Direction.E;
}
else {
this.Direction = Direction.W;
value = Math.Abs(value);
}
}
this.Degrees = (int)Math.Floor(value);
this.Minutes = (int)Math.Floor((value % 1) * 60);
this.Seconds = (int)Math.Round((((value % 1) * 60) % 1) * 60);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment