代码如下
大家自己看吧
[HTML] 纯文本查看 复制代码 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=写你自己的"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
<title>百度地图的Hello, World</title>
<style type="text/css">
body, html, #allmap { width: 100%; height: 100%; overflow: hidden; margin: 0; }
</style>
</head>
<body>
<div id="allmap">
</div>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(113.691872, 34.784988);
map.centerAndZoom(point, 12);
map.enableScrollWheelZoom();
map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
var ggPoint = new BMap.Point(113.691872, 34.784988);
BMap.Convertor.translate(ggPoint, 2,getPoint);
function getPoint(point) {
point//就是转化后的百度经纬度了
}
</script>
C#实现方式
[C#] 纯文本查看 复制代码 /// <summary>
/// 百度经纬度转化帮助类
/// </summary>
public class BaiduMapHelper
{
/// <summary>
/// 百度地理位置转化URl
/// </summary>
private static string CoordsUrl = "http://api.map.baidu.com/geoconv/v1/";
/// <summary>
/// 根据输入的经纬度List转化为百度地图的经纬度List,一一对应
/// </summary>
/// <param name="citem">要转化的经纬度List</param>
/// <returns>转化后的经纬度List</returns>
public static List<CoordsItem> GetCoords(List<CoordsItem> citem)
{
HttpHelper http = new HttpHelper();
string strCoords = ListToString(citem);
HttpItem item = new HttpItem()
{
URL = string.Format("{0}?coords={1}&from=3&to=5&ak=17bc43866bbd51f7507e0c618f890e64", CoordsUrl, strCoords),//URL 必需项
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
citem = StringToList(html);
return citem;
}
/// <summary>
/// 根据输入的经纬度转化为百度地图的经纬度
/// </summary>
/// <param name="citem">要转化的经纬度</param>
/// <returns>转化后的经纬度List</returns>
public static CoordsItem GetCoords(CoordsItem citem)
{
List<CoordsItem> list = new List<CoordsItem>();
list.Add(citem);
list = GetCoords(list);
if (list != null & list.Count > 0)
{
return list[0];
}
return null;
}
/// <summary>
/// 根据List转化为相应的字符串经纬度
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private static string ListToString(List<CoordsItem> item)
{
StringBuilder strCoords = new StringBuilder();
foreach (var obj in item)
{
if (string.IsNullOrWhiteSpace(strCoords.ToString()))
{
strCoords.Append(obj.X + "," + obj.Y);
}
else
{
strCoords.Append(";" + obj.X + "," + obj.Y);
}
}
return strCoords.ToString();
}
//{"status":0,"result":[{"x":114.22539195429,"y":29.581585367458},{"x":114.2253919533,"y":29.581585366942}]}
//{"status":24,"message":"param error:coords format error","result":[]}
/// <summary>
/// 根据Json格式转化为List
/// </summary>
/// <param name="str">Json字符串</param>
/// <returns></returns>
private static List<CoordsItem> StringToList(string str)
{
List<CoordsItem> item = new List<CoordsItem>();
//json to list
object obj = JsonHelper.jsonDes<CoordsList>(str);
CoordsList c = (CoordsList)obj;
return c.result;
}
}
/// <summary>
/// 返回参数类
/// </summary>
public class CoordsList
{
/// <summary>
/// 状态0为成功其他不成功
/// </summary>
public int status { get; set; }
/// <summary>
/// result不为0时才显示
/// </summary>
public string message { get; set; }
/// <summary>
/// 经纬度列表
/// </summary>
public List<CoordsItem> result { get; set; }
}
/// <summary>
/// 经纬度类
/// </summary>
public class CoordsItem
{
/// <summary>
/// 经
/// </summary>
public double X { get; set; }
/// <summary>
/// 纬
/// </summary>
public double Y { get; set; }
}
这个类很有用的,大家可以试试,回头我会写入到基类库当中
|