简体   繁体   English

获取Windows 8桌面应用程序中的位置

[英]Getting location in Windows 8 desktop apps

I am a total beginner at C#, but I have used Java a lot. 我是C#的初学者,但我经常使用Java。 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: 我正在制作一个Windows 8桌面应用程序,以便在我的设备中使用GPS传感器:

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. 'await'要求'Windows.Foundation.IAsyncOperation'类型具有合适的GetAwaiter方法。 Are you missing a using directive for 'System'? 你错过了'系统'的使用指令吗? C:\\Users\\clidy\\documents\\visual studio 2012\\Projects\\Hello-Location\\Hello-Location\\Form1.cs 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. 如果您可以为我指向C#位置的一些资源和Windows 桌面应用程序的传感器API,那么它将非常有用。 Upon googling, I am only getting Windows RT APIs. 谷歌搜索时,我只获得Windows RT API。

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: 如果您使用的是Windows运行时事件处理程序等映射类型,则可能还需要添加对System.Runtime.WindowsRuntime.dll的引用:

... ...

That assembly resides in C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework.NETCore\\v4.5 该程序集位于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 . 我最近找到了一个类似问题的“ 解决方案 ”: C#桌面应用程序不共享我的物理位置 Maybe you might be interested by my approach: https://stackoverflow.com/a/14645837/674700 . 也许您可能对我的方法感兴趣: 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. 它更像是一种解决方法,并不是针对Windows 8,但它最终会起作用。

alex's solution works! alex的解决方案有效! add that reference and geolocation api starts working like a charm! 添加该引用和地理位置api开始像魅力一样工作! 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM