地理位置位置总是未知的,我该如何修复?

发布于 2025-01-17 13:16:29 字数 1799 浏览 0 评论 0 原文

我正在尝试使用 system.device.location.location 来获取用户的位置,但是每次运行程序时,GeoCoortion的Isunknown属性始终是正确的。我不确定为什么我也打开了系统。请帮助我解决此问题。

这是我到目前为止尝试的。

static void GetLocationProperty()
        {
            CivicAddress civic = new CivicAddress();
            GeoCoordinate watcher = new GeoCoordinate();

            if (watcher.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}, region:{2}", watcher.Latitude, watcher.Longitude, civic.CountryRegion);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }
        }

它总是给出 watcher.isunknown 是正确的。我在这里做错了什么,还是一些权限访问问题?

我正在使用.NET Framework 4.6.1和Visual Studio 2017。

我还尝试了此代码从MSDN网站上

using System;
using System.Device.Location;

namespace GetLocationProperty
{
    class Program
    {
        static void Main(string[] args)
        {
            GetLocationProperty();
        }

        static void GetLocationProperty()
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

            // Do not suppress prompt, and wait 1000 milliseconds to start.
            watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

            GeoCoordinate coord = watcher.Position.Location;

            if (coord.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}",
                    coord.Latitude,
                    coord.Longitude);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }
        }
    }
}

尝试了此错误

错误CS0246:找不到类型或名称名称'GeoCoortionWatcher'(您是否缺少使用指令或汇编引用?)

I'm trying to get the location of the user using the System.Device.Location but every time I run the program GeoCoordinate's IsUnknown property is always true. I'm not sure why it's always unknown I've turned on my system as well. Please help me fix this issue.

Here's what I tried so far.

static void GetLocationProperty()
        {
            CivicAddress civic = new CivicAddress();
            GeoCoordinate watcher = new GeoCoordinate();

            if (watcher.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}, region:{2}", watcher.Latitude, watcher.Longitude, civic.CountryRegion);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }
        }

It always gives watcher.IsUnknown is true. Am I doing something wrong here or is it some permission access issue?

I am using .NET Framework 4.6.1 and Visual Studio 2017.

I have also tried this code from the MSDN website

using System;
using System.Device.Location;

namespace GetLocationProperty
{
    class Program
    {
        static void Main(string[] args)
        {
            GetLocationProperty();
        }

        static void GetLocationProperty()
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

            // Do not suppress prompt, and wait 1000 milliseconds to start.
            watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

            GeoCoordinate coord = watcher.Position.Location;

            if (coord.IsUnknown != true)
            {
                Console.WriteLine("Lat: {0}, Long: {1}",
                    coord.Latitude,
                    coord.Longitude);
            }
            else
            {
                Console.WriteLine("Unknown latitude and longitude.");
            }
        }
    }
}

but I get this error

error CS0246: The type or namespace name 'GeoCoordinateWatcher' could not be found (are you missing a using directive or an assembly reference?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2025-01-24 13:16:29

如果您看一下地理位置默认构造函数您可以看到文档。

初始化一个未设置数据字段的新型地理位置实例。

IE地理位分角是一种数据传输对象,它仅表示坐标,而不是从GPS或其他来源生成此类坐标的方法。为此,您可能想要。请参阅如何获得纬度和经度< /a>

If you take a look at the geoCoordinate default constructor documentation you can see.

Initializes a new instance of GeoCoordinate that has no data fields set.

I.e. a GeoCoordinate is a kind of data transfer object, it only represents a coordinate, not a way to generate such a coordinate from GPS or other sources. For that you probably want GeoCoordinateWatcher. See How to get Latitude and Longitude

哭了丶谁疼 2025-01-24 13:16:29

当您尝试从DVICE获得本地化时,您需要使用真实的观察器( geocoortionWatcher ):

编辑:似乎OP System在定位正确的GeoCoorectionWatcher时遇到了一些麻烦我建议明确的路由

记住:

using System;
using System.Device.Location;

改用此代码:

    static void GetLocationProperty()
    {
        System.Device.Location.GeoCoordinateWatcher watcher = new System.Device.Location.GeoCoordinateWatcher();

        // Do not suppress prompt, and wait 1000 milliseconds to start.
        watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

        System.Device.Location.GeoCoordinate coord = watcher.Position.Location;

        if (coord.IsUnknown != true)
        {
            Console.WriteLine("Lat: {0}, Long: {1}",
                coord.Latitude,
                coord.Longitude);
        }
        else
        {
            Console.WriteLine("Unknown latitude and longitude.");
        }
    }

As you are trying to get the Localization from dvice you need to use the real watcher (GeoCoordinateWatcher):

EDIT: as seems that OP system is having some trouble locating the correct GeoCoordinateWatcher I propose explicit route

remember using:

using System;
using System.Device.Location;

And use this code instead:

    static void GetLocationProperty()
    {
        System.Device.Location.GeoCoordinateWatcher watcher = new System.Device.Location.GeoCoordinateWatcher();

        // Do not suppress prompt, and wait 1000 milliseconds to start.
        watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

        System.Device.Location.GeoCoordinate coord = watcher.Position.Location;

        if (coord.IsUnknown != true)
        {
            Console.WriteLine("Lat: {0}, Long: {1}",
                coord.Latitude,
                coord.Longitude);
        }
        else
        {
            Console.WriteLine("Unknown latitude and longitude.");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文