一,经纬度的获取
1. 微软定位
2. 谷歌定位
3. 高德定位
二. 地名的获取(通过一获取的经纬度获取)
1.百度数据接口
2.洛基亚数据接口
3.高德地图
三.实现
1.0 获取经纬度
开始前注意勾选开发程序所支持的ID_CAP_LOCATION功能,一定要勾选。
(1)微软定位:
/// <summary>
/// 微软定位经纬度
/// </summary>
private async void GetLocationMic()
{
// 实例化Geolocator对象,以获取用户所在的地理位置
Geolocator geolocator = new Geolocator();
// 设置定位精度
geolocator.DesiredAccuracyInMeters = 50;
// 设置当用户移动超过多少米的时候,触发位置改变事件
geolocator.MovementThreshold = 500;
Geoposition position = await geolocator.GetGeopositionAsync(
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10));
try
{
// 获取经纬度
entity.longitude = position.Coordinate.Point.Position.Longitude;
entity.latitude = position.Coordinate.Point.Position.Latitude;
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("定位失败!" + ex.Message.ToString());
});
}
}
(2)谷歌定位:
/// <summary>
/// 谷歌定位经纬度
/// </summary>
private async void GetLocation()
{
// 实例化Geolocator对象,以获取用户所在的地理位置
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));
GeoCoordinate coord = watcher.Position.Location;
try
{
if (coord.IsUnknown != true)
{
entity.latitude = coord.Latitude;
entity.longitude = coord.Longitude;
}
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("定位失败。");
});
}
}
(3)高德定位:实现下列代码首先要做的准备是
【1】注册高德地图账号,获取开发程序所需的KEY。点击打开链接
【2】下载最新的示例包(高德地图 Windows Phone SDK V2.2.1)。
【3】添加引用Com.AMap.Api.Maps.dll和Com.AMap.Api.Services.dll,在下载包中的AMap_WP_Api_Demo_V2.2.0\AMAP3DEngine\references文件夹中,里面有两个文件夹,arm是真机所要的引用,x86是虚拟机所要的引用。我的Demo中也有,在Bin文件夹下。获取经纬度只需要添加这两个引用,如果实现其他的功能看它提供的Demo。
【4】在要开发的程序的App.cs文件的构造函数中加入下面代码
///定位使用高德地图定位,此处为实现定位所加代码
string myMapKey = "申请的KEY";
///连接高德地图所需要的Key,在高德官网申请,一个程序一个key:http://lbs.amap.com/console/key
AMapConfig.Key = myMapKey;//地图显示key
AMapSearchConfig.Key = myMapKey;
AMapGeolocator mylocation;
private void GetLocationGaode()
{
mylocation = new AMapGeolocator();
mylocation.Start();
//触发位置改变事件
mylocation.PositionChanged += mylocation_PositionChanged;
}
private void mylocation_PositionChanged(AMapGeolocator sender, AMapPositionChangedEventArgs args)
{
this.Dispatcher.BeginInvoke(()=> {
entity.latitude = args.LngLat.latitude;//纬度
entity.longitude = args.LngLat.longitude;//经度
});
}
他们三个获取经纬度,谷歌和微软获取的经纬度的精确度要比高德获取的高很多。这里没有百度地图的获取,WP被百度给无情的抛弃了,
2.0 获取地名
(1) 百度数据接口获取
【1】获取链接所需的AK 。需要先成为它的开发人员,然后再“查看应用”中点击“创建应用”,输入应用名,然后再‘Refer白名单:“中输入“*”点击确认就能看到AK。 点击打开链接
private async void GetAddresssFromBadide(double latitude, double longitude)
{
GetCityName();
response = new HttpResponseMessage();
Uri resourceUri;
String Address = "http://api.map.baidu.com/geocoder/v2/?ak=<span style="color:#3366FF;">申请的AK</span>&location=" + latitude + "," + longitude + "&output=json&pois=0";
Uri.TryCreate(Address.Trim(), UriKind.Absolute, out resourceUri);
string responseText = null;
try
{
response = await httpClient.GetAsync(resourceUri);
response.EnsureSuccessStatusCode();
responseText = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("网络请求失败!" + ex.Message.ToString());
});
}
JsonObject city = JsonObject.Parse(responseText);
JsonObject result = city["result"].GetObject();
JsonObject addressComponent = result["addressComponent"].GetObject();
string province = addressComponent.GetNamedString("province");
string citys = addressComponent.GetNamedString("city");
string district = addressComponent.GetNamedString("district");
entity .address = province + citys + district;
}
(2) 洛基亚数据接口(只是这里获取的地名是英文---拼音地名)
/// <summary>
/// 传入经纬度到洛基亚地图数据 接口获取地名
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
private async void GetCityNnameFromNOKIAMap(double latitude, double longitude)
{
GetCityName();
response = new HttpResponseMessage();
Uri resourceUri;
Uri.TryCreate((MapNOKIAUrl + latitude + "&long=" + longitude).Trim(), UriKind.Absolute, out resourceUri);
string responseText = null;
try
{
response = await httpClient.GetAsync(resourceUri);
response.EnsureSuccessStatusCode();
responseText = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("网络请求失败!" + ex.Message.ToString());
});
return;
}
JObject jObject = null;
try
{
JObject json = JObject.Parse(responseText);
//判断获取数据是否失败,为错误代码
string resultCode = Convert.ToString(json["resultCode"]);//如果获取成功则返回OK
if (!resultCode.Equals("OK"))
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("定位不到当前的地名!" );
});
return;
}
JArray array = (JArray)json["places"];
jObject = (JObject)array[0]["address"];
}
catch (Exception)
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("定位不到当前的地名!");
});
return;
}
string city = jObject["city"] == null ? "" : jObject["city"].ToString();//市
string district = jObject["district"] == null ? "" : jObject["district"].ToString();//区县
JObject jbject = (JObject)jObject["thoroughfare"];//
string name = jbject["name"] == null ? "" : jbject["name"].ToString();//路
entity.address = city + district + name;
}
(3) 高德地图( 这个有问题,建议不用看这个)
/// <summary>
/// 高德获取地名
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
private async void GeoCodeToAddress(double latitude, double longitude)
{
try
{
//请求查询
AMapReGeoCodeResult rcc = await AMapReGeoCodeSearch.GeoCodeToAddress(longitude, latitude, 500, "", Com.AMap.Api.Services.Extensions.All );
if (rcc.Erro == null && rcc.ReGeoCode != null)
{
AMapReGeoCode regeocode = rcc.ReGeoCode;
this.Dispatcher.BeginInvoke(() =>
{
entity.address = regeocode.Formatted_address;
});
}
}
catch (Exception e)
{
this.Dispatcher.BeginInvoke(() => {
MessageBox.Show("高德获取地名失败。\n" +e.ToString ());
});
}
}