当我尝试下面将粘贴的代码时,它返回城市的空白,并返回country = US,但我住在比利时.
根据这个link
它说:
位置服务提供对位置功能的访问,例如小区三角测量,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=234238namespace 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属性)设置,也许这可能是有用的,使它更具体一些.
总结以上是内存溢出为你收集整理的c# – Geolocation.CivicAddress.City返回空的win8 metro app全部内容,希望文章能够帮你解决c# – Geolocation.CivicAddress.City返回空的win8 metro app所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)