• 百度、谷歌根据地理位置获取经纬度


    /// <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";
    
    }
    }
  • 相关阅读:
    eclipse用法和技巧
    eclipse常用快捷键集锦
    移动端input的虚拟键盘影响布局
    使用github page + Hexo搭建个人博客折腾记
    javascript数组的排序(sort,冒泡)
    响应式布局与媒体查询
    css属性选择器诸如Class^=,Class*= ,Class$=释义
    怎么预览 GitHub 项目里的网页或 Demo
    常见浏览器的兼容问题(一)
    jQuery常用交互效果
  • 原文地址:https://www.cnblogs.com/zhaojingwei/p/9151232.html
Copyright © 2020-2023  润新知