Windows Phone GetGeopositionAsync()返回错误的位置

时间:2015-10-19 21:37:13

标签: c# windows-phone-8 location

我有一个按钮,只要按下它就会调用CurrentLocation

private async void CurrentLocation()
{
    try
    {
        Geolocator myGeolocator = new Geolocator();
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(3));
        Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
        GeoCoordinate myGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
        this.MyMap.Center = myGeoCoordinate;
    }
    catch
    { }

}

我一直在Windows Phone模拟器上测试应用程序,一切正常。但是今天当我在驾驶汽车时按下Lumina 640上运行的应用程序按钮时,应用程序开始显示不同的位置。

任何人都知道我的代码可能出现了什么问题?

编辑:

Construcor

public MainPage()
{
    InitializeComponent();
    distanceTextBox.Visibility = Visibility.Collapsed;

    CreateStandardApplicationBar();

    pointNamePrompt = new InputPrompt()
    {
        Title = "Point",
        Message = "Name the point",
    };
    try
    {
        CurrentLocation();
        MyMap.ZoomLevel = 10;
    }
    catch { }

    LoadAppSettings();
}

按钮

private void CurrentLocation_Click(object sender, EventArgs e)
{
    CurrentLocation();
}

最后新的新代码在应用程序启动时仍然可以正常工作:

private async void CurrentLocation()
{
    try
    {
        Geolocator myGeolocator = new Geolocator();
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(10));

        ReverseGeocodeQuery query = new ReverseGeocodeQuery();
        query.GeoCoordinate = new System.Device.Location.GeoCoordinate(myGeoposition.Coordinate.Latitude, myGeoposition.Coordinate.Longitude);

        query.QueryCompleted += (s, e) =>
        {
            if (e.Error != null && e.Result.Count == 0)
                return;
            MessageBox.Show(e.Result[0].Information.Address.PostalCode);
        };
        query.QueryAsync();

        double lat = 0.00, lng = 0.00;
        lat = Convert.ToDouble(myGeoposition.Coordinate.Latitude);
        lng = Convert.ToDouble(myGeoposition.Coordinate.Longitude);

        this.MyMap.Center = new GeoCoordinate(lat, lng);
    }
    catch
    { }

}

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

private async void CurrentLocation()
{
    try
    {
        Geolocator myGeolocator = new Geolocator();
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5),timeout: TimeSpan.FromSeconds(10));

        ReverseGeocodeQuery query = new ReverseGeocodeQuery();
        query.GeoCoordinate = new System.Device.Location.GeoCoordinate(myGeoposition.Coordinate.Latitude, myGeoposition.Coordinate.Longitude);

        query.QueryCompleted += (s, e) =>
        {
            if (e.Error != null && e.Result.Count == 0)
                return;
            MessageBox.Show(e.Result[0].Information.Address.PostalCode);
        };
        query.QueryAsync();

        double lat = 0.00, lng = 0.00;
        lat = Convert.ToDouble(myGeoposition.Coordinate.Latitude);
        lng = Convert.ToDouble(myGeoposition.Coordinate.Longitude);

        this.MyMap.Center = new GeoCoordinate(lat, lng);
        this.MyMap.ZoomLevel = 7;
        this.MyMap.Show();
    }
    catch
    { }

}