简体   繁体   中英

Geolocation.CivicAddress.City returns empty win8 metro app

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM