Geolocation.CivicAddress.City返回空win8 metro应用程序(Geolocation.CivicAddress.City returns empty win8 metr

编程入门 行业动态 更新时间:2024-10-21 13:27:49
Geolocation.CivicAddress.City返回空win8 metro应用程序(Geolocation.CivicAddress.City returns empty win8 metro app)

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

根据这个链接它说:位置服务提供对位置功能的访问,例如细胞三角测量,WiFi(通过IP地址)和GPS。 同时很多现代设备都支持以前所述的某种方式解析位置,应用程序必须处理位置服务无法解析位置或用户已从控制面板禁用位置服务的情况。

我的笔记本电脑没有GPS,但使用IP,它应该知道城市和国家。

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

I want to create a simple app that shows me the city of the current application. When I tried the code that I will paste below it returns empty for city, and it returns for country =US, but I live in Belgium.

According to this link It says: The location services provides access to location functionality, such as cell triangulations, WiFi (through IP address), and GPS. Also great many modern devices supports resolving location in some way from mentioned before, application must handle the case where location services cannot resolve the location or user has disabled location services from the Control Panel.

My Laptop does not have GPS, but with the IP, it should know the city and country.

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

最满意答案

相当肯定的是,您需要等待Geolocator实际获得一个位置。

天真的方法是只是不断尝试在一个while循环,看看是否有新的更新。

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

以下是源代码中的一些信息和代码示例。

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

我相信那里也有一些准确性(DesiredAccuracy属性)设置,也许这​​可能会有用,并使它更具体一些。

Fairly certain you'll need to wait for the Geolocator to actually get a position.

The naive way would be to just keep trying in a while loop to see if there's a new update.

You'll want to probably want to attach to the PositionChanged event handler and wait for that to tell you that you have new updates.

Here's some info and code examples straight from the source.

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

I do believe there's also some accuracy (DesiredAccuracy property) settings in there, perhaps that could be useful as well in making it be a bit more specific.

更多推荐

本文发布于:2023-08-03 14:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   City   Geolocation   CivicAddress   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!