wp wp8:lbs

阅读更多
上码:不解释

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using wp8Lbs.Resources;
using System.Device.Location;
using System.IO;

namespace wp8Lbs
{
    public partial class MainPage : PhoneApplicationPage
    {
        //URL接口来自诺基亚地图 
        private const String CITY_INFO_URI = "http://loc.desktop.maps.svc.ovi.com/geocoder/rgc/1.0?lat={0}&long={1}&output=json";

        public MainPage()
        {
            InitializeComponent();

            StartLocationService(GeoPositionAccuracy.Default);
        }

        private void StartLocationService(GeoPositionAccuracy accuracy)
        {
            GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(accuracy);
            watcher.MovementThreshold = 20;

            watcher.StatusChanged += new EventHandler(watcher_StatusChanged);
            watcher.PositionChanged += new EventHandler>(watcher_PositionChanged);

            watcher.Start();
        }

        //INVOKE是同步函数会阻塞住用户的UI线程换句话说如果用INVOKE来做可能造成用户
        //界面卡,而BeginInvoke是异步的函数会在时间片空闲的时间被调用
        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));

        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));

        }

        void MyPositionChanged(GeoPositionChangedEventArgs e)
        {

            System.Diagnostics.Debug.WriteLine(e.Position.Location.Latitude.ToString("0.000"));

            System.Diagnostics.Debug.WriteLine(e.Position.Location.Longitude.ToString("0.000"));

            //访问此uri会得到json城市信息数据
            this.getCityInfo(e.Position.Location.Latitude, e.Position.Location.Longitude);
          

        }

        void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    System.Diagnostics.Debug.WriteLine("location is unsupported on this device");
                    break;
                case GeoPositionStatus.Initializing:
                    System.Diagnostics.Debug.WriteLine("initializing location service");
                    break;
                case GeoPositionStatus.NoData:
                    System.Diagnostics.Debug.WriteLine("data unavailable");
                    break;
                case GeoPositionStatus.Ready:
                    System.Diagnostics.Debug.WriteLine("receiving data");
                    break;

            }
        }
       
        ///
        /// 获取城市信息
        ///

        /// 经度
        /// 纬度
        public void getCityInfo(double latitude, double longitude)
        {
            string urlString = String.Format(CITY_INFO_URI, latitude, longitude);

            Uri uri = new Uri(urlString, UriKind.RelativeOrAbsolute);

            System.Diagnostics.Debug.WriteLine(uri);

            WebClient webClient = new WebClient();

            //显示中文信息

            webClient.Headers["Accept-Language"] = "zh-CN,zh;q=0.8";

            webClient.OpenReadAsync(uri);

            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_openReadComplete);

        }

        void webClient_openReadComplete(object sender, OpenReadCompletedEventArgs e)
        {

            try
            {

                using (StreamReader reader = new StreamReader(e.Result))
                {
                    String contents = reader.ReadToEnd();

                    System.Diagnostics.Debug.WriteLine(contents);

                }

            }
            catch (Exception)
            {
               
            }

        }


    }
}

你可能感兴趣的:(wp)