Redigera

Dela via


GeoCoordinateWatcher.Start Method

Definition

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

Overloads

Start()

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

Start(Boolean)

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

Start()

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

public:
 virtual void Start();
public void Start ();
abstract member Start : unit -> unit
override this.Start : unit -> unit
Public Sub Start ()

Implements

Examples

The following program handles the first location update that occurs after Start is called.

using System;
using System.Device.Location;

namespace GetLocationDataUpdateOnce
{
    class Program
    {
        static void Main(string[] args)
        {
            CLocation myLocation = new CLocation();
            myLocation.GetLocationDataEvent();
            Console.WriteLine("Enter any key to quit.");
            Console.ReadLine();
        }
        class CLocation
        {
            GeoCoordinateWatcher watcher;

            public void GetLocationDataEvent()
            {
                this.watcher = new GeoCoordinateWatcher();
                this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                this.watcher.Start();
            }

            void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
            {
                PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);
                // Stop receiving updates after the first one.
                this.watcher.Stop();
            }

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

Module GetLocationEvent
    Public Class CLocation
        Private WithEvents watcher As GeoCoordinateWatcher
        Public Sub GetLocationDataEvent()
            watcher = New System.Device.Location.GeoCoordinateWatcher()
            AddHandler watcher.PositionChanged, AddressOf watcher_PositionChanged
            watcher.Start()

        End Sub

        Private Sub watcher_PositionChanged(ByVal sender As Object, ByVal e As GeoPositionChangedEventArgs(Of GeoCoordinate))
            PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude)
            ' Stop receiving updates after the first one.
            watcher.Stop()
        End Sub

        Private Sub PrintPosition(ByVal Latitude As Double, ByVal Longitude As Double)
            Console.WriteLine("Latitude: {0}, Longitude {1}", Latitude, Longitude)
        End Sub
    End Class


    Public Sub Main()
        Dim myLocation As New CLocation()
        myLocation.GetLocationDataEvent()
        Console.WriteLine("Enter any key to quit.")
        Console.ReadLine()
    End Sub

End Module

Remarks

Calling this method will initiate the acquisition of data from the current location provider. The current location provider is selected based on factors such as the age and accuracy of the data from all providers, the accuracy requested by the application or applications, and the power consumption and performance impact associated with the location provider. The current location provider might change over time, for instance, when a GPS device loses its satellite signal indoors and a Wi-Fi triangulation provider becomes the most accurate provider on the computer.

If the current prioritized location provider does not have data when the Start method is called, it will start to acquire data. If the permissions have been granted to the client when the data becomes available, data can be accessed synchronously, and will be delivered asynchronously if events are being handled.

If the Windows 7 Sensor and Location platform is disabled when Start is called, Start will immediately return, PositionChanged events will not be raised, and the location returned by the Location property of Position will contain Unknown.

If the current prioritized location provider does have data, it will be available synchronously immediately, and will be delivered asynchronously if events are being handled.

If the calling application does not have permissions to access data from a location provider, the user will be prompted with a dialog box to either grant or deny permission. The dialog box will be modeless.

Applies to

Start(Boolean)

Initiate the acquisition of data from the current location provider. This method enables PositionChanged events and allows access to the Position property.

public:
 virtual void Start(bool suppressPermissionPrompt);
public void Start (bool suppressPermissionPrompt);
abstract member Start : bool -> unit
override this.Start : bool -> unit
Public Sub Start (suppressPermissionPrompt As Boolean)

Parameters

suppressPermissionPrompt
Boolean

true to suppress the permission dialog box; false to optionally show the permission dialog box if permissions have not already been granted.

Implements

Applies to