GeoCoordinateWatcher.TryStart在C#中无法正常工作 [英] GeoCoordinateWatcher.TryStart doesn't work properly in C#

查看:134
本文介绍了GeoCoordinateWatcher.TryStart在C#中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有问题,即它没有显示权限提示,因此我无法授予应用程序访问我的位置的权限,并且不起作用.

I'm having an issue with the following code, namely that it doesn't show a Permission Prompt, therefore I can't give the application access to my location and doesn't work.

public Tuple<double, double> GetDeviceLocation()
{
    GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();

    watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));

    GeoCoordinate coord = watcher.Position.Location;

    if (coord.IsUnknown != true)
    {
        return Tuple.Create(coord.Latitude, coord.Longitude);
    }
}

推荐答案

我看过Microsoft文档,并且有一些吸引我的注意力.

I've looked microsoft documents and there's something that catches my attention.

相对于坐标必须移动的距离(以米为单位)从上一个PositionChanged事件开始,在位置提供者之前引发另一个PositionChanged事件.

The distance that must be moved, in meters, relative to the coordinate from the last PositionChanged event, before the location provider raises another PositionChanged event.

备注

默认运动阈值为零,这意味着任何当前位置提供商检测到的位置变化会导致PositionChanged事件和Position属性的更新.

The default movement threshold is zero, which means that any change in location detected by the current location provider causes a PositionChanged event and an update in the Position property.

   // Get location
   CLocation myLocation = new CLocation();
   myLocation.GetLocationEvent();

您必须注册 PostionChange 事件以获取位置信息

You must be register PostionChange event to get location information

在需要的地方使用位置信息类(CLocation).

Use the location class (CLocation) where do you need.

地理位置信息 MSDN

    public class CLocation
   {
    GeoCoordinateWatcher watcher;

    public void GetLocationEvent()
    {
        this.watcher = new GeoCoordinateWatcher();
        this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
        bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(2000));
        if (!started)
        {
            Console.WriteLine("GeoCoordinateWatcher timed out on start.");
        }
    }

    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);
    }

    void PrintPosition(double Latitude, double Longitude)
    {
        Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude);
    }
}

这篇关于GeoCoordinateWatcher.TryStart在C#中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆