Прочетете на английски Редактиране

Споделяне чрез


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.

C#
public void Start();

Implements

Examples

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

C#
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);
            }
        }
    }
}

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

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Start(Boolean)

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

C#
public void Start(bool suppressPermissionPrompt);

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

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1