2015-10-19 3 views
1

У меня есть кнопка, которая вызывает CurrentLocation всякий раз, когда она нажата.Windows Phone GetGeopositionAsync() возвращает неправильное местоположение

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, и все работает нормально. Но сегодня, когда я нажал кнопку в приложении, работающем на Lumina 640 во время езды в машине, приложение начало показывать разные местоположения.

Кто-нибудь знает, что может быть неправильным в моем коде?

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

Кнопка

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

Пытаться ниже кода:

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

} 
+0

@miechooy, если мое приложение-оповещатель выполнит ваш вопрос, тогда примите это. –

+0

спасибо, что я проведу это вечером! :) – miechooy

+0

Используется на emualtor - постоянно отображается в одном месте; ( – miechooy

Смежные вопросы