{ "status": 0, "result": [ { "x": 39.926674689976, "y": 116.46594011987 }, { "x": 40.136798619397, "y": 117.10587935376 } ] }根据官方的说明,我们传入几个坐标,转换后就会得到几个坐标,并且是一样的顺序。为了能够更多好的操作数据,我们需要将其转换到相应的类中,所以首先要构造相应的类,然后将数据反序列化到该类中(这里要用到.net的json库Newtonsoft.Json.dll,这个可以到网上自行下载)。
/// <summary> /// 百度坐标的转换结果 /// </summary> [Serializable] public class CoordTransResult { /// <summary> /// 状态 /// </summary> public CoordTransStatus status { get; set; } /// <summary> /// 结果(坐标数组) /// </summary> public Coordinate[] result { get; set; } } public enum CoordTransStatus { /// <summary> /// 正常 /// </summary> OK = 0, /// <summary> /// 内部错误 /// </summary> INTERNAL_ERROR = 1, /// <summary> /// from非法 /// </summary> FROM_ILLEGAL = 21, /// <summary> /// to非法 /// </summary> TO_ILLEGAL = 22, /// <summary> /// coords格式非法 /// </summary> COORDS_ILLEGAL = 24, /// <summary> /// coords个数非法,超过限制 /// </summary> COORDS_COUNT_ILLEGAL = 25 } /// <summary> /// 坐标 /// </summary> [Serializable] public class Coordinate { public Coordinate() { } public Coordinate(String x, String y) { this.x = x; this.y = y; } public String x { get; set; } public String y { get; set; } }这些是构造出的相关类。
/// <summary> /// 转换成百度坐标 /// </summary> /// <param name="coordinates">坐标(经度,纬度),多个坐标间用分号隔开</param> /// <param name="mapCoordinateType">坐标类型</param> /// <returns></returns> public static CoordTransResult TransToBaiduCoord(String coordinates, MapCoordinateType mapCoordinateType = MapCoordinateType.GOOGLE_SOSO_ALIYUN_MAPABC_AMAP) { String transformUrl = String.Format(TRANSFORM_COORDINATE_URL_TEMPLEATE, MAP_KEY_BAI_DU, coordinates, (int)mapCoordinateType, (int)MapCoordinateType.BAIDU); String transformResponsText = RequestHelper.RequestUrl(transformUrl, null); CoordTransResult transformResult = null; String info = ""; try { transformResult = Newtonsoft.Json.JsonConvert.DeserializeObject<CoordTransResult>(transformResponsText); } catch (Exception e) { info = "转换坐标异常:" + e.Message; return null; } return transformResult; }下面是测试代码
protected void btnTest_Click(object sender, EventArgs e) { String coordinates = "39.92,116.46;40.13,117.10"; CoordTransResult coordTransResult= BaiduMap.TransToBaiduCoord(coordinates, MapCoordinateType.GOOGLE_SOSO_ALIYUN_MAPABC_AMAP); Alert.Show(coordTransResult.status.ToString()); }测试结果如下
原文地址:http://blog.csdn.net/xxdddail/article/details/42705195