c# - Geolocation.CivicAddress.City 返回空的 win8 metro 应用程序

标签 c# location windows-8 microsoft-metro

我想创建一个简单的应用程序来显示当前应用程序所在的城市。 当我尝试我将粘贴在下面的代码时,它返回空的城市,它返回国家 =US,但我住在比利时。

根据这个link 它说: 位置服务提供对位置功能的访问,例如小区三角测量、WiFi(通过 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;
            }
        }
    }
}

最佳答案

可以肯定的是,您需要等待地理定位器实际获得位置。

天真的方法是在 while 循环中不断尝试以查看是否有新的更新。

您可能想要附加到 PositionChanged 事件处理程序并等待它告诉您有新的更新。

这里有一些直接来自源代码的信息和代码示例。

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

我确实相信那里还有一些准确性(DesiredAccuracy 属性)设置,也许这​​对于使其更具体一些也很有用。

关于c# - Geolocation.CivicAddress.City 返回空的 win8 metro 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10968660/

相关文章:

c# - 在Asp.net C#中使用jQuery Ajax插入数据[数据库MySQL]

javascript - navigator.getCurrentPosition() 在 Firefox 或 Safari 中不起作用

css - 如何使用灵活的布局将元素置于 Windows 8 [HTML5/JS/CSS3] 中

c# - 尝试写入空十进制值时出现 Filehelpers NullReferenceException

c# - 从 Winforms 中的不同函数访问控件

swift - 调用 requestWhenInUseAuthorization 获取位置权限时出错

xaml - 为 Metro 应用程序的 Bing map 上的标记图钉设置动画

.net - WinRT/Win8 中的 HttpClient 缺少响应 header

c# - 协助打开从 C# VSTO Excel 项目到关闭的 .xlsx 文件的连接(不打开 .xlsx 文件)

仅限locationmanager的Android源代码?