/// <summary> /// 根据地理位置获取经纬度 /// </summary> /// <param name="address"></param> /// <returns></returns> public static string GetBaiduLocation(string address) { string latlng = ""; try { String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=xml&ak=SyGHfOU5utqzhgtng68LZ0CGOMdIsI5O"; { WebRequest request = WebRequest.Create(url); request.Method = "POST"; XmlDocument xmlDoc = new XmlDocument(); string sendData = xmlDoc.InnerXml; byte[] byteArray = Encoding.Default.GetBytes(sendData); Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("utf-8")); string responseXml = reader.ReadToEnd(); XmlDocument xml = new XmlDocument(); xml.LoadXml(responseXml); string status = xml.DocumentElement.SelectSingleNode("status").InnerText; if (status == "0") { double lng = double.Parse(xml.DocumentElement.GetElementsByTagName("lng")[0].InnerText.ToString()); double lat = double.Parse(xml.DocumentElement.GetElementsByTagName("lat")[0].InnerText.ToString()); return latlng = lng + "," + lat; } else { return "0,0"; } } } catch (System.Exception ex) { return "0,0"; } } /// <summary> /// 根据地理位置获取经纬度 /// </summary> /// <param name="address"></param> /// <returns></returns> public static string GetGoogleLocation(string address) { string latlng = ""; try { String url = "https://ditu.google.cn/maps/api/geocode/xml?address=" + address + "&key=AIzaSyA69pcp_xUEfUmKnWjlY6jCIAH2aqTNiX8&signed=true"; { WebRequest request = WebRequest.Create(url); request.Method = "POST"; XmlDocument xmlDoc = new XmlDocument(); string sendData = xmlDoc.InnerXml; byte[] byteArray = Encoding.Default.GetBytes(sendData); Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, System.Text.Encoding.GetEncoding("utf-8")); string responseXml = reader.ReadToEnd(); XmlDocument xml = new XmlDocument(); xml.LoadXml(responseXml); string status = xml.DocumentElement.SelectSingleNode("status").InnerText; if (status == "OK") { double lng = double.Parse(xml.DocumentElement.GetElementsByTagName("lng")[0].InnerText.ToString()); double lat = double.Parse(xml.DocumentElement.GetElementsByTagName("lat")[0].InnerText.ToString()); return latlng = lat + "," + lng; } else { return "0,0"; } } } catch (System.Exception ex) { return "0,0"; } }