Skip to content

Commit 35af7cd

Browse files
committed
Add LocationHelper #110
1 parent b7c71ae commit 35af7cd

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

dev/DevWinUI.Gallery/Views/Pages/Common/SunTimesHelperPage.xaml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,30 @@
3636
</StackPanel>
3737
</local:ControlExample.Pane>
3838
<StackPanel Spacing="10">
39+
<InfoBar Title="Retrieve your current Latitude and Longitude"
40+
IsClosable="False"
41+
IsOpen="True"
42+
Message="Works only if location access is granted and your device supports reliable location services (e.g., GPS or Wi-Fi). If your device does not have GPS, results may be inaccurate.">
43+
<InfoBar.ActionButton>
44+
<Button x:Name="BtnGetLocation"
45+
Click="BtnGetLocation_Click"
46+
Content="Get Latitude and Longitude" />
47+
</InfoBar.ActionButton>
48+
</InfoBar>
49+
<InfoBar x:Name="InfoBarSource"
50+
Title="Position Source"
51+
IsClosable="False"
52+
IsOpen="True"
53+
Message="Position source will appear here after retrieving location."
54+
Severity="Warning" />
3955
<Button x:Name="BtnCalc"
4056
Click="BtnCalc_Click"
41-
Content="Calculate" />
57+
Content="Calculate Sunrise and Sunset"
58+
Style="{StaticResource AccentButtonStyle}" />
4259
<TextBlock x:Name="TxtSunRise"
43-
Style="{ThemeResource BaseTextBlockStyle}" />
60+
Style="{ThemeResource TitleTextBlockStyle}" />
4461
<TextBlock x:Name="TxtSunSet"
45-
Style="{ThemeResource BaseTextBlockStyle}" />
62+
Style="{ThemeResource TitleTextBlockStyle}" />
4663
</StackPanel>
4764
</local:ControlExample>
4865
</StackPanel>

dev/DevWinUI.Gallery/Views/Pages/Common/SunTimesHelperPage.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@ private void BtnCalc_Click(object sender, RoutedEventArgs e)
1313
TxtSunRise.Text = $"Sunrise: {sunTimes.SunriseHour}:{sunTimes.SunriseMinute}";
1414
TxtSunSet.Text = $"Sunset: {sunTimes.SunsetHour}:{sunTimes.SunsetMinute}";
1515
}
16+
17+
private async void BtnGetLocation_Click(object sender, RoutedEventArgs e)
18+
{
19+
var pos = await LocationHelper.GetGeoLocationAsync();
20+
if (pos == null)
21+
return;
22+
23+
NBLatitude.Value = pos.Coordinate.Point.Position.Latitude;
24+
NBLongitude.Value = pos.Coordinate.Point.Position.Longitude;
25+
26+
InfoBarSource.Message = pos.Coordinate.PositionSource.ToString();
27+
}
1628
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Windows.Devices.Geolocation;
2+
3+
namespace DevWinUI;
4+
5+
public partial class LocationHelper
6+
{
7+
public static async Task<Geoposition> GetGeoLocationAsync()
8+
{
9+
return await GetGeoLocationAsync(PositionAccuracy.Default, null);
10+
}
11+
public static async Task<Geoposition> GetGeoLocationAsync(PositionAccuracy accuracy)
12+
{
13+
return await GetGeoLocationAsync(accuracy, null);
14+
}
15+
public static async Task<Geoposition> GetGeoLocationAsync(PositionAccuracy accuracy, uint? desiredAccuracyInMeters)
16+
{
17+
try
18+
{
19+
// Request access
20+
var accessStatus = await Geolocator.RequestAccessAsync();
21+
if (accessStatus != GeolocationAccessStatus.Allowed)
22+
{
23+
throw new InvalidOperationException($"Location access failed: {accessStatus}");
24+
}
25+
26+
var geolocator = new Geolocator { DesiredAccuracy = accuracy };
27+
28+
if (desiredAccuracyInMeters.HasValue)
29+
geolocator.DesiredAccuracyInMeters = desiredAccuracyInMeters;
30+
31+
return await geolocator.GetGeopositionAsync();
32+
}
33+
catch (Exception ex)
34+
{
35+
throw new Exception(ex.Message);
36+
}
37+
}
38+
}

dev/DevWinUI/Helpers/SunTimes/SunTimesHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,4 @@ static double NormalizeHours(double hours)
121121
return hours;
122122
}
123123
}
124-
125124
}

0 commit comments

Comments
 (0)