protected void Page_Load(object sender, EventArgs e)
{
//获取当前纬度 string Latitude = "117.707677"; //获取当前经度 string Longitude = "39.084097"; //百度坐标转换API string path = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + Latitude + "&y=" + Longitude + ""; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(path); webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8); string strJson = sr.ReadToEnd(); JavaScriptSerializer js = new JavaScriptSerializer(); MapConvert mc = js.Deserialize<MapConvert>(strJson); if (mc.error == "0") { string lon = mc.x; string lat = mc.y; //进行Base64解码 byte[] xBuffer = Convert.FromBase64String(lon); string strX = Encoding.UTF8.GetString(xBuffer, 0, xBuffer.Length); byte[] yBuffer = Convert.FromBase64String(lat); string strY = Encoding.UTF8.GetString(yBuffer, 0, xBuffer.Length); string strB = strX + "," + strY; } else { string strB = "获取失败"; } } } public class MapConvert { public string error { get; set; } public string x { get; set; } public string y { get; set; } }
时间: 2024-11-10 00:40:55