2012-06-10 52 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

相关问题