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<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)
{

}

}


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值