Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

.net - GeoCoordinateWatcher.TryStart doesn't work properly in 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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Remarks

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();

You must be register PostionChange event to get location information

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

GeoLocation Information 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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...