wp wp8:lbs
程序员文章站
2022-04-30 10:02:41
...
上码:不解释
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<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(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<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
}
void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> 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;
}
}
/// <summary>
/// 获取城市信息
/// </summary>
/// <param name="latitude">经度</param>
/// <param name="longitude">纬度</param>
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)
{
}
}
}
}
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<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(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<GeoCoordinate> e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
}
void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> 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;
}
}
/// <summary>
/// 获取城市信息
/// </summary>
/// <param name="latitude">经度</param>
/// <param name="longitude">纬度</param>
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)
{
}
}
}
}
推荐阅读
-
WP8.1中IE浏览器存在重大漏洞:用户保存的密码极易泄露
-
windows phone 配置PhoneGap开发环境(wp7 phonegap 开发环境)图文教程
-
Win10 Mobile降级WP8.1无法登录微软账户怎么办?官方工具惹的祸?
-
Win10 Mobile 10512与WP8.1性能体验对比视频
-
w3wp.exe占用cpu过高的解决方法第1/2页
-
Win10手机预览版回滚WP8.1刷机工具更新下载:修复Bug
-
Win10手机版回滚WP8.1刷机工具更新到v3.1.2:支持LG Lancet
-
如何在WP7上用XNA写2D游戏(二)
-
WP7自定义控件 评分控件
-
详解WordPress开发中wp_title()函数的用法