【问题标题】:Windows Phone GetGeopositionAsync() returns wrong locationWindows Phone GetGeopositionAsync() 返回错误的位置
【发布时间】:2015-10-19 21:37:13
【问题描述】:

我有一个按钮,只要按下它就会调用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 上运行的应用程序中的按钮时,应用程序开始显示不同的位置。

有人知道我的代码有什么问题吗?

编辑:

构造器

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

}

【问题讨论】:

    标签: c# windows-phone-8 location


    【解决方案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
        { }
    
    }
    

    【讨论】:

    • @miechoy 如果我的回答满足您的问题,请接受。
    • 在模拟器上使用 - 始终显示相同的位置;(
    • 您是否更改了您当前的位置,或者您与以前的位置相同?
    • 是的,使用模拟器管理器 { 编辑:重新打开应用程序后,它向我显示了新位置,让我检查它是否可以工作}
    • 你在尝试哪个代码?请检查到设备中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多