在Windows Phone 7应用程序中获取经度和纬度的问题

时间:2011-09-08 09:48:25

标签: windows-phone-7

我正在使用GeoCoordinateWatcher类来获取Windows Phone 7的经纬度,但是当我在Windows手机上调试此应用程序时, 我在StatusChanged事件中收到GeoPositionStatus.NoData。请告诉我以下代码有什么问题。

GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
watcher.MovementThreshold = 10.0f;
// wire up event handlers
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_statusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

void watcher_statusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
    switch (e.Status)
    {
        case GeoPositionStatus.Disabled:
            if (watcher.Permission == GeoPositionPermission.Denied)
            {
                // the user has disabled LocServ on their device.
                statusTextBlock.Text = "You have disabled Location Service.";
            }
            else
            {
                statusTextBlock.Text = "Location Service is not functioning on this device.";
            }
        break;

        case GeoPositionStatus.Initializing:
            // The location service is initializing.
            statusTextBlock.Text = "Location Service is retrieving data...";
        break;

        case GeoPositionStatus.NoData:
            // The Location Service is working, but it cannot get location data
            // due to poor signal fidelity (most likely)
            statusTextBlock.Text = "Location data is not available.";
        break;

        case GeoPositionStatus.Ready:
            // The location service is working and is receiving location data.
            statusTextBlock.Text = "Location data is available.";
        break;
    }
}

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs <GeoCoordinate> e)
{
    // update the textblock readouts.
    latitudeTextblock.Text = e.Position.Location.Latitude.ToString("0.0000000000");
    longitudeTextblock.Text = e.Position.Location.Longitude.ToString("0.0000000000");
    speedreadout.Text = e.Position.Location.Speed.ToString("0.0") + " meters per second";
    coursereadout.Text = e.Position.Location.Course.ToString("0.0") + " degrees";
    altitudereadout.Text = e.Position.Location.Altitude.ToString("0.0") + " meters above sea level";
}

2 个答案:

答案 0 :(得分:1)

你应该启动观察者。

watcher.Start(...);

答案 1 :(得分:1)

我发现您发现位置服务已关闭。但需要记住的是,在认证中,他们会检查您的程序是否可以处理该方案。启动位置服务后,您可以查看是否已禁用。

watcher.Start(); bool IsLocationServicesTurnedOff =(watcher.Permission == GeoPositionPermission.Denied);

另请注意,在Mango中,您的程序可以暂停,禁用位置服务,并重新激活您的程序。换句话说,可以在程序的生命周期中随时禁用位置服务。