getgepositionasync()返回错误的位置

本文关键字:位置 错误 返回 getgepositionasync | 更新日期: 2023-09-27 18:18:35

我有一个按钮,每次按下它都会调用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
    { }
}

getgepositionasync()返回错误的位置

试试下面的代码:

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
    { }
}