/// <summary> /// 坐标互转 /// </summary> /// <param name="lng">经度</param> /// <param name="lat">纬度</param> /// <param name="from">待转换的坐标类型</param> /// <param name="to">最终转换成的坐标类型</param> /// <returns></returns> public static Map ConvertAll(double lng, double lat, MapEnum from, MapEnum to) { //google 坐标转百度链接 //http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=&y= //gps坐标的type=0 //google坐标的type=2 //baidu坐标的type=4 string path = "http://api.map.baidu.com/ag/coord/convert?from=" + from.GetHashCode() + "&to=" + to.GetHashCode() + "&x=" + lng + "+&y=" + lat; //使用http请求获取转换结果、得到返回的结果 string res = HttpHelper.GET(path).ToString(); //处理结果 MapJson json = JsonHelper.JsonDeserialize <MapJson>(res); Map map = new Map(); if (json.X != null) { byte[] outputb = Convert.FromBase64String(json.X); map.X = Convert.ToDouble(Encoding.Default.GetString(outputb)); } if (json.Y != null) { byte[] outputb = Convert.FromBase64String(json.Y); map.Y = Convert.ToDouble(Encoding.Default.GetString(outputb)); } return(map); }
/// <summary> /// 根据百度坐标获取地址 /// </summary> /// <param name="lng">经度</param> /// <param name="lat">纬度</param> public static string XY2Address(double lng, double lat) { string res = HttpHelper.GET("http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&location=" + lat + "," + lng + "&output=xml&pois=0").ToString(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(res); XmlNode note = xmlDoc.SelectSingleNode("//GeocoderSearchResponse//result//formatted_address"); return(note.InnerText); }
/// <summary> /// 根据地址返回百度坐标 /// </summary> /// <param name="address">地址</param> /// <param name="city">搜索在城市,如:上海市</param> /// <returns></returns> public static Map Address2BXBY(string address, string city) { Map json = new Map(); string url = "http://api.map.baidu.com/geocoder/v2/?ak=" + ak + "&output=xml&address=" + address + "&city=" + city; string res = HttpHelper.GET(url).ToString(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(res); XmlNode note = xmlDoc.SelectSingleNode("//GeocoderSearchResponse//result//location//lng"); if (!string.IsNullOrEmpty(note.InnerText)) { json.X = Convert.ToDouble(note.InnerText); } note = xmlDoc.SelectSingleNode("//GeocoderSearchResponse//result//location//lat"); if (!string.IsNullOrEmpty(note.InnerText)) { json.Y = Convert.ToDouble(note.InnerText); } return(json); }