I’m trying to get the Coordinate of current location of my device using “GeoCoordinateWatcher” Class and feed it to a weather API query. But after installation of System.Device.Location.Portable package, I have following build error:
C:WorkGitHubWeatherPanelMainWindow.xaml.cs(38,36,38,56): error CS0234: The type or namespace name 'GeoCoordinateWatcher' does not exist in the namespace 'System.Device.Location' (are you missing an assembly reference?)
warning:
warning NU1701: Package 'System.Device.Location.Portable 1.0.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net6.0-windows10.0.19041'. This package may not be fully compatible with your project.
How to resolve this build error, or is there any other Nuget package to install to make GeoCoordinateWatcher code work? thanks.
MainWindow.xaml.cs:
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System;
using System.Device.Location;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WeatherPanel
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
_ = Main();
}
async Task Main()
{
GeoCoordinateWatcher watcher;
watcher = new GeoCoordinateWatcher();
watcher.PositionChanged += (sender, e) =>
{
var coordinate = e.Position.Location;
Console.WriteLine("Lat: {0}, Long: {1}", coordinate.Latitude,
coordinate.Longitude);
// Uncomment to get only one event.
watcher.Stop();
};
string apiKey = "YOUR_API_KEY";
string city = "NewYork";
//string location = "48.8567,2.3508"
string apiUrl = $"http://api.weatherapi.com/v1/current.json?key={apiKey}&q={city}&aqi=no";
try
{
string weatherData = await GetWeatherData(apiUrl);
// Parse and extract relevant information from the JSON response
// For simplicity, let's assume you're interested in the temperature only
string temperature = ParseValues(weatherData, "temp_c");
txtTemp.Text = "Today : " + temperature.ToString() + " °C";
string currentStatus = ParseValues(weatherData, "text");
txtStatus.Text = currentStatus.ToString();
string precipitation = ParseValues(weatherData, "precip_in");
txtPrecip.Text = "Precipitation : " + precipitation.Substring(1,precipitation.Length-1).ToString() + " in";
string wind = ParseValues(weatherData, "wind_mph");
txtWind.Text = "Wind : " + wind.ToString() + " mph";
string humidity = ParseValues(weatherData, "humidity");
txtHumidity.Text = "Humidity : " + humidity.ToString();
string feelsLike = ParseValues(weatherData, "feelslike_c");
txtFeelsLike.Text = "Feels Like : " + feelsLike.ToString() + " °C";
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
Project Source Code :
https://github.com/Ron391/WeatherPanel