C#的百度地图开发(三)依据坐标获取位置、商圈及周边信息
2015-06-04 09:17
447 查看
我们得到了百度坐标,现在依据这一坐标来获取相应的信息。下面是相应的代码
[html] view plaincopy
public class BaiduMap
{
/// <summary>
/// 依据坐标获取定位信息的URL模板。
/// 参数1:百度地图API的KEY。
/// 参数2:坐标(经度,纬度)。
/// </summary>
public const string GEOCODING_COORDINATE_URL_TEMPLATE =
"http://api.map.baidu.com/geocoder/v2/?ak={0}&location={1}&output=json&pois=1";
/// <summary>
/// 依据坐标获取定位信息
/// </summary>
/// <param name="coordinates">坐标(经度,纬度),多个坐标间用分号隔开</param>
/// <param name="mapCoordinateType">坐标类型</param>
/// <returns></returns>
public static CoordLocationResult FetchLocation(String coordinates,
MapCoordinateType mapCoordinateType)
{
CoordTransResult transformResult = TransToBaiduCoord(coordinates, mapCoordinateType);
String info = "";
if (!transformResult.status.Equals(CoordTransStatus.OK))
{
info = "坐标转换异常:状态是---" + transformResult.status.ToString();
return null;
}
if (transformResult.result == null || transformResult.result.Length <= 0)
{
info = "坐标转换异常:结果为空或数组长度为0";
return null;
}
String locationUrl = "";
foreach (Coordinate coordTemp in transformResult.result)
{
locationUrl = String.Format(GEOCODING_COORDINATE_URL_TEMPLATE,
MAP_KEY_BAI_DU,
coordTemp.x + "," + coordTemp.y);
}
String locationResponseText = RequestHelper.RequestUrl(locationUrl, null);
CoordLocationResult locationResult = null;
try
{
locationResult = Newtonsoft.Json.JsonConvert.DeserializeObject<CoordLocationResult>(locationResponseText);
}
catch (Exception e)
{
info = "定位异常:" + e.Message;
return null;
}
return locationResult;
}
}
注:
(1).使用const常量来定义一个百度地图API的URL模板,方便后面的调用。
(2).TransToBaiduCoord函数是《C#的百度地图开发(二)转换JSON数据为相应的类》中将非百度坐标转换成百度坐标方法的封装。
(3).RequestUrl方法是《C#的百度地图开发(一)发起HTTP请求》所说的发起HTTP请求的封装。
(4).CoordLocationResult类的具体实现,请参看后面的代码。
[html] view plaincopy
namespace MapApi.Baidu
{
[Serializable]
public class CoordLocationResult
{
/// <summary>
/// 状态
/// </summary>
public String status { get; set; }
/// <summary>
/// 结果
/// </summary>
public CoordLocationResult_Result result { get; set; }
}
#region CoordLocationResult_Result
/// <summary>
/// 定位结果
/// </summary>
[Serializable]
public class CoordLocationResult_Result
{
/// <summary>
/// 定位的经度与纬度
/// </summary>
public CoordLocationResult_Result_Location location { get; set; }
/// <summary>
/// 结构化地址信息
/// </summary>
public String formatted_address { get; set; }
/// <summary>
/// 所在商圈信息,如 "人民大学,中关村,苏州街"
/// </summary>
public String business { get; set; }
/// <summary>
/// 定位的行政区域
/// </summary>
public CoordLocationResult_Result_AddressComponent addressComponent { get; set; }
/// <summary>
/// 周边位置
/// </summary>
public CoordLocationResult_Result_Poi[] pois { get; set; }
/// <summary>
/// 周边区域
/// </summary>
public CoordLocationResult_Result_PoiRegion[] poiRegions { get; set; }
/// <summary>
/// 城市代码
/// </summary>
public String cityCode { get; set; }
}
/// <summary>
/// 定位结果之定位的经纬度
/// </summary>
[Serializable]
public class CoordLocationResult_Result_Location
{
/// <summary>
/// 经度
/// </summary>
public String lng { get; set; }
/// <summary>
/// 纬度
/// </summary>
public String lat { get; set; }
}
/// <summary>
/// 定位结果之行政区域
/// </summary>
[Serializable]
public class CoordLocationResult_Result_AddressComponent
{
/// <summary>
/// 城市名
/// </summary>
public String city { get; set; }
/// <summary>
/// 区县名
/// </summary>
public String district { get; set; }
/// <summary>
/// 省名
/// </summary>
public String province { get; set; }
/// <summary>
/// 街道名
/// </summary>
public String street { get; set; }
/// <summary>
/// 街道门牌号
/// </summary>
public String street_number { get; set; }
}
#endregion
#region CoordLocationResult_Result_Poi
/// <summary>
/// 周边位置信息
/// </summary>
[Serializable]
public class CoordLocationResult_Result_Poi
{
//"addr": "福建省厦门市湖里区嘉禾路388",
// "cp": "NavInfo",
// "direction": "西",
// "distance": "49",
// "name": "永同昌大厦",
// "poiType": "商务大厦",
// "point": {
// "x": 118.13374113945,
// "y": 24.501871673827
// },
// "tel": "",
// "uid": "19c4b3f2642893beafb22a1e",
// "zip": ""
/// <summary>
/// 地址信息
/// </summary>
public String addr { get; set; }
/// <summary>
/// 数据来源
/// </summary>
public String cp { get; set; }
/// <summary>
/// 方向
/// </summary>
public String direction { get; set; }
/// <summary>
/// 离坐标点距离
/// </summary>
public String distance { get; set; }
/// <summary>
/// poi名称
/// </summary>
public String name { get; set; }
/// <summary>
/// poi类型,如’办公大厦,商务大厦’
/// </summary>
public String poiType { get; set; }
/// <summary>
/// poi坐标{x,y}
/// </summary>
public Coordinate point { get; set; }
/// <summary>
/// 电话
/// </summary>
public String tel { get; set; }
/// <summary>
/// poi唯一标识
/// </summary>
public String uid { get; set; }
/// <summary>
/// 邮编
/// </summary>
public String zip { get; set; }
}
#endregion
#region CoordLocationResult_Result_PoiRegion
/// <summary>
/// 周边区域
/// </summary>
[Serializable]
public class CoordLocationResult_Result_PoiRegion
{
/// <summary>
/// 目标方向。比如:内
/// </summary>
public String direction_desc { get; set; }
/// <summary>
/// 区域名称。比如:音乐·家生活广场
/// </summary>
public String name { get; set; }
}
#endregion
}
注:类的构造方法依据前面所说的构造,也可以使用工具直接生成(链接)。
下面是测试代码
[html] view plaincopy
protected void btnTest_Click(object sender, EventArgs e)
{
Coordinate coordinate = new Coordinate("39.92", "116.46");
CoordLocationResult coordLocationResult=BaiduMap.FetchLocation(coordinate);
Alert.Show(coordLocationResult.status.ToString());
}
测试结果如下
从图中可以看到,formatted_address是位置信息,business是商圈信息,pois是周围的信息,其他的信息可自行参考百度地图WebApi的官方文档说明。
这样,我们就得到了指定坐标点的位置信息,那得到了这些信息后,如果在前面的地图上显示呢?
[html] view plaincopy
public class BaiduMap
{
/// <summary>
/// 依据坐标获取定位信息的URL模板。
/// 参数1:百度地图API的KEY。
/// 参数2:坐标(经度,纬度)。
/// </summary>
public const string GEOCODING_COORDINATE_URL_TEMPLATE =
"http://api.map.baidu.com/geocoder/v2/?ak={0}&location={1}&output=json&pois=1";
/// <summary>
/// 依据坐标获取定位信息
/// </summary>
/// <param name="coordinates">坐标(经度,纬度),多个坐标间用分号隔开</param>
/// <param name="mapCoordinateType">坐标类型</param>
/// <returns></returns>
public static CoordLocationResult FetchLocation(String coordinates,
MapCoordinateType mapCoordinateType)
{
CoordTransResult transformResult = TransToBaiduCoord(coordinates, mapCoordinateType);
String info = "";
if (!transformResult.status.Equals(CoordTransStatus.OK))
{
info = "坐标转换异常:状态是---" + transformResult.status.ToString();
return null;
}
if (transformResult.result == null || transformResult.result.Length <= 0)
{
info = "坐标转换异常:结果为空或数组长度为0";
return null;
}
String locationUrl = "";
foreach (Coordinate coordTemp in transformResult.result)
{
locationUrl = String.Format(GEOCODING_COORDINATE_URL_TEMPLATE,
MAP_KEY_BAI_DU,
coordTemp.x + "," + coordTemp.y);
}
String locationResponseText = RequestHelper.RequestUrl(locationUrl, null);
CoordLocationResult locationResult = null;
try
{
locationResult = Newtonsoft.Json.JsonConvert.DeserializeObject<CoordLocationResult>(locationResponseText);
}
catch (Exception e)
{
info = "定位异常:" + e.Message;
return null;
}
return locationResult;
}
}
注:
(1).使用const常量来定义一个百度地图API的URL模板,方便后面的调用。
(2).TransToBaiduCoord函数是《C#的百度地图开发(二)转换JSON数据为相应的类》中将非百度坐标转换成百度坐标方法的封装。
(3).RequestUrl方法是《C#的百度地图开发(一)发起HTTP请求》所说的发起HTTP请求的封装。
(4).CoordLocationResult类的具体实现,请参看后面的代码。
[html] view plaincopy
namespace MapApi.Baidu
{
[Serializable]
public class CoordLocationResult
{
/// <summary>
/// 状态
/// </summary>
public String status { get; set; }
/// <summary>
/// 结果
/// </summary>
public CoordLocationResult_Result result { get; set; }
}
#region CoordLocationResult_Result
/// <summary>
/// 定位结果
/// </summary>
[Serializable]
public class CoordLocationResult_Result
{
/// <summary>
/// 定位的经度与纬度
/// </summary>
public CoordLocationResult_Result_Location location { get; set; }
/// <summary>
/// 结构化地址信息
/// </summary>
public String formatted_address { get; set; }
/// <summary>
/// 所在商圈信息,如 "人民大学,中关村,苏州街"
/// </summary>
public String business { get; set; }
/// <summary>
/// 定位的行政区域
/// </summary>
public CoordLocationResult_Result_AddressComponent addressComponent { get; set; }
/// <summary>
/// 周边位置
/// </summary>
public CoordLocationResult_Result_Poi[] pois { get; set; }
/// <summary>
/// 周边区域
/// </summary>
public CoordLocationResult_Result_PoiRegion[] poiRegions { get; set; }
/// <summary>
/// 城市代码
/// </summary>
public String cityCode { get; set; }
}
/// <summary>
/// 定位结果之定位的经纬度
/// </summary>
[Serializable]
public class CoordLocationResult_Result_Location
{
/// <summary>
/// 经度
/// </summary>
public String lng { get; set; }
/// <summary>
/// 纬度
/// </summary>
public String lat { get; set; }
}
/// <summary>
/// 定位结果之行政区域
/// </summary>
[Serializable]
public class CoordLocationResult_Result_AddressComponent
{
/// <summary>
/// 城市名
/// </summary>
public String city { get; set; }
/// <summary>
/// 区县名
/// </summary>
public String district { get; set; }
/// <summary>
/// 省名
/// </summary>
public String province { get; set; }
/// <summary>
/// 街道名
/// </summary>
public String street { get; set; }
/// <summary>
/// 街道门牌号
/// </summary>
public String street_number { get; set; }
}
#endregion
#region CoordLocationResult_Result_Poi
/// <summary>
/// 周边位置信息
/// </summary>
[Serializable]
public class CoordLocationResult_Result_Poi
{
//"addr": "福建省厦门市湖里区嘉禾路388",
// "cp": "NavInfo",
// "direction": "西",
// "distance": "49",
// "name": "永同昌大厦",
// "poiType": "商务大厦",
// "point": {
// "x": 118.13374113945,
// "y": 24.501871673827
// },
// "tel": "",
// "uid": "19c4b3f2642893beafb22a1e",
// "zip": ""
/// <summary>
/// 地址信息
/// </summary>
public String addr { get; set; }
/// <summary>
/// 数据来源
/// </summary>
public String cp { get; set; }
/// <summary>
/// 方向
/// </summary>
public String direction { get; set; }
/// <summary>
/// 离坐标点距离
/// </summary>
public String distance { get; set; }
/// <summary>
/// poi名称
/// </summary>
public String name { get; set; }
/// <summary>
/// poi类型,如’办公大厦,商务大厦’
/// </summary>
public String poiType { get; set; }
/// <summary>
/// poi坐标{x,y}
/// </summary>
public Coordinate point { get; set; }
/// <summary>
/// 电话
/// </summary>
public String tel { get; set; }
/// <summary>
/// poi唯一标识
/// </summary>
public String uid { get; set; }
/// <summary>
/// 邮编
/// </summary>
public String zip { get; set; }
}
#endregion
#region CoordLocationResult_Result_PoiRegion
/// <summary>
/// 周边区域
/// </summary>
[Serializable]
public class CoordLocationResult_Result_PoiRegion
{
/// <summary>
/// 目标方向。比如:内
/// </summary>
public String direction_desc { get; set; }
/// <summary>
/// 区域名称。比如:音乐·家生活广场
/// </summary>
public String name { get; set; }
}
#endregion
}
注:类的构造方法依据前面所说的构造,也可以使用工具直接生成(链接)。
下面是测试代码
[html] view plaincopy
protected void btnTest_Click(object sender, EventArgs e)
{
Coordinate coordinate = new Coordinate("39.92", "116.46");
CoordLocationResult coordLocationResult=BaiduMap.FetchLocation(coordinate);
Alert.Show(coordLocationResult.status.ToString());
}
测试结果如下
从图中可以看到,formatted_address是位置信息,business是商圈信息,pois是周围的信息,其他的信息可自行参考百度地图WebApi的官方文档说明。
这样,我们就得到了指定坐标点的位置信息,那得到了这些信息后,如果在前面的地图上显示呢?
相关文章推荐
- C#中this的用法集锦
- C#获取邮件客户端保存的邮箱密码
- C# 并行编程 之 PLINQ 规约操作和聚合函数
- C# webservice初探
- C#技术漫谈之垃圾回收机制(GC)
- [C#]高性能计时器的实现
- [C#]系列文章——关于C#,你应该知道的2000件事情(001)
- C#匿名类型
- C#构造函数的声明、重载与继承调用
- C# 复制(深拷贝、浅拷贝)
- C#获取当前堆栈的各调用方法列表
- C#中汉字排序简单示例(拼音/笔划)
- C# string byte[] Base64 常用互相转换
- C#实现windows form拷贝内容到剪贴板的方法
- C# 下实现UPD编程Demo
- 【C#】读书笔记
- C# Task的使用---Task的启动
- C#设计模式(1)——单例模式
- Random 类
- C#控制台输出菱形