Geolocation.CivicAddress.City returns empty win8 app

I want to create a simple application that will show me the city of the current application. When I tried the code, which I will insert below, it returns an empty value for the city, and it returns for the country = USA, but I live in Belgium.

Accordingly, the link It states: Location services provide access to location functions such as cell triangulation, WiFi (via IP address) and GPS. In addition, many modern devices somehow support location resolution, the application must handle the case when location services cannot resolve the location, or the user has disabled location services from the control panel.

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

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; } } } } 
+4
source share
1 answer

Sure enough that you will need to wait until the Geolocator really gets a position.

A naive way would be to simply try in a while loop to see if there is a new update.

You will probably want to attach the PositionChanged event handler and wait for this to inform you that you have new updates.

Here are some examples of information and code directly from the source.

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

I really believe that there is some accuracy (the DesiredAccuracy property), maybe this can be useful in making it more specific.

+1
source

All Articles