4

現在のアプリケーションの都市を表示するシンプルなアプリを作成したいと考えています。以下に貼り付けるコードを試してみると、都市では空が返され、国 = US では返されますが、私はベルギーに住んでいます。

このリンク によると、次のように書かれています。位置情報サービスは、セル三角測量、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;
            }
        }
    }
}
4

1 に答える 1

1

Geolocator が実際に位置を取得するまで待つ必要があることはほぼ確実です。

単純な方法は、while ループを試行し続けて、新しい更新があるかどうかを確認することです。

おそらく、PositionChanged イベント ハンドラーにアタッチし、それが新しい更新があることを通知するのを待ちたいと思うでしょう。

ソースからの情報とコード例をいくつか示します。

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

そこにはいくつかの精度 (DesiredAccuracy プロパティ) の設定もあると思います。おそらく、もう少し具体的にするのにも役立つでしょう。

于 2012-06-19T13:46:08.640 に答える