2012-06-10 51 views
4

我想創建一個簡單的應用程序,顯示當前應用程序的城市。 當我嘗試下面粘貼的代碼時,它將返回空的城市,並返回國家=美國,但我住在比利時。Geolocation.CivicAddress.City返回空win8 metro應用程序

根據這一link 它說: 位置服務提供了訪問定位功能,如細胞的三角測量,無線網絡連接(通過IP地址)和GPS。同時很多現代設備都支持以前所述的某種方式解析位置,應用程序必須處理位置服務無法解析位置或用戶已從控制面板禁用位置服務的情況。

我的筆記本沒有GPS,但有了IP,它應該知道城市和國家。

enter image description here

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using Windows.Devices.Geolocation; 
using System.Threading.Tasks; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 

namespace AlarmPro 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 


     public MainPage() 
     { 
      this.InitializeComponent(); 

      //InitializeLocationServices(); 
     } 

     /// <summary> 
     /// Invoked when this page is about to be displayed in a Frame. 
     /// </summary> 
     /// <param name="e">Event data that describes how this page was reached. The Parameter 
     /// property is typically used to configure the page.</param> 
     protected async override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      TextBlock txt = new TextBlock(); 
      var location = await InitializeLocationServices(); 
      txt.Text = location; 

      Grid.SetRow(txt, 0); 
      Grid.SetColumn(txt, 1); 

     } 

     private async Task<string> InitializeLocationServices() 
     { 
      //Initialize geolocator object 
      Geolocator geoLocator = new Geolocator(); 
      try 
      { 
       //Try resolve the current location 
       var position = await geoLocator.GetGeopositionAsync(); 
       if (position !=null) 
       { 
        string city = position.CivicAddress.City; 
        string country = position.CivicAddress.Country; 
        string state = position.CivicAddress.State; 
        string zip = position.CivicAddress.PostalCode; 
        string msg = "I am located in " + country; 
        if (city.Length > 0) 
         msg += ", city of " + city; 
        if (state.Length > 0) 
         msg += ", " + state; 
        if (zip.Length > 0) 
         msg += " near zip code " + zip; 
        return msg; 
       } 
       return string.Empty; 
      } 
      catch (Exception) 
      { 
       //Nothing to do - no GPS signal or some timeout occured.n . 
       return string.Empty; 
      } 
     } 
    } 
} 
+0

是的,地理定位在控制面板上啓用。但在該地區,更改位置設置爲美國,我將其更改爲比利時,現在我的代碼顯示我是在該國,但仍然是城市是空的 –

+0

您是否使用任何代理? –

+0

我的家庭設置沒有任何代理,我在我的ISP下。 –

回答

1

相當肯定,你需要等待Geolocator實際得到的位置。

天真的方法是隻是在while循環中試圖查看是否有新的更新。

你可能想要附加到PositionChanged事件處理程序,並等待它告訴你,你有新的更新。

以下是一些直接來自源代碼的信息和代碼示例。

http://msdn.microsoft.com/en-us/library/windows/apps/br225534.aspx

我相信也有一定的準確性(DesiredAccuracy屬性)在那裏設置,也許這​​可能是有用的,以及在使之成爲一個更具體一點。

+0

雖然,我現在認識到你在那裏等着。我的印象是,它試圖不阻擋,並會嘗試在背景中獲得更好的準確性。我會在這裏嘗試,但我還沒有Win8測試盒。 – Breland

+0

Breland,在這種情況下,等待正確執行。我非常懷疑這是個問題。 – EkoostikMartin

+0

意識到我發佈後。嗯,當我回到家時,我有一個虛擬機,我會很樂意地開火,並且希望有人會比我有更多的知識。 – Breland

相關問題