Getting location in Windows 8 desktop apps

2020-03-04 03:26发布

问题:

I am a total beginner at C#, but I have used Java a lot. I am trying to use the following code in my app to get location data. I am making a Windows 8 desktop app to use the GPS sensor in my device:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Sensors;
using Windows.Devices.Geolocation;
using Windows.Devices.Geolocation.Geoposition;
using Windows.Foundation;

namespace Hello_Location
{
    public partial class Form1 :
    {
        public Form1()
        {
            InitializeComponent();
        }

        async private void Form1_Load(object sender, EventArgs e)
        {
            Geolocator loc = new Geolocator();
            try
            {
                loc.DesiredAccuracy = PositionAccuracy.High;
                Geoposition pos = await loc.GetGeopositionAsync();
                var lat = pos.Coordinate.Latitude;
                var lang = pos.Coordinate.Longitude;
                Console.WriteLine(lat+ " " +lang);
            }
            catch (System.UnauthorizedAccessException)
            {
                // handle error
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

I get this error:

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'? C:\Users\clidy\documents\visual studio 2012\Projects\Hello-Location\Hello-Location\Form1.cs

How can I fix this?

Also it will be very useful if you can point me to some resources for C# location and the sensor API for windows desktop apps. Upon googling, I am only getting Windows RT APIs.

回答1:

To fix your error you have to reference to the link that Bart gave in one of the question's comments.

You might need to add a reference to System.Runtime.WindowsRuntime.dll as well if you are using mapped types like Windows Runtime event handlers:

...

That assembly resides in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5

I recently found a "solution" for a similar question: C# desktop application doesn't share my physical location. Maybe you might be interested by my approach: https://stackoverflow.com/a/14645837/674700.

It's more like a workaround and it's not targeting Windows 8, but it works in the end.



回答2:

alex's solution works! add that reference and geolocation api starts working like a charm! so do async methods for other sensors!

here is a function i just got working using it.

async public void UseGeoLocation()
{
    Geolocator _GeoLocator = new Geolocator();
    Geoposition _GeoPosition = 
        await _GeoLocator.GetGeopositionAsync();

    Clipboard.Clear();
    Clipboard.SetText("latitude," + 
        _GeoPosition.Coordinate.Latitude.ToString() + 
        "," + "longitude," + _GeoPosition.Coordinate.Longitude.ToString() + 
        "," + "heading," + _GeoPosition.Coordinate.Heading.ToString() +
        "," + "speed," + _GeoPosition.Coordinate.Speed.ToString());

    Application.Exit();
}