miechooy
miechooy

Reputation: 3422

Windows Phone GetGeopositionAsync() returns wrong location

I have a button which calls CurrentLocation whenever it is pressed.

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

}

I've been testing the app on the Windows Phone emulator and everything was working fine. But today while I was pressing the button in the app running on a Lumina 640 while driving in a car, the app began showing different locations.

Anyone know what what might be wrong in my code?

EDIT:

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();
}

Button

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

And finally new new code which still work just for the first time when app starts:

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

}

Upvotes: 1

Views: 83

Answers (1)

Krunal Mevada
Krunal Mevada

Reputation: 1655

Try below code:

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

}

Upvotes: 1

Related Questions