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


    /// <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";
    
    }
    }
  • 相关阅读:
    学习 iOS多线程开发 Demo示意
    python(数字Number)
    python(元组)
    python(列表)
    python(一)
    APP自动化测试框架搭建(转载)
    Appium真机运行Device Name获取方法
    关于android sdk manager在下载包时没有android intel x86 atom system image等项
    adb shell命令查看当前与用户交互的activity
    SDK更新
  • 原文地址:https://www.cnblogs.com/zhaojingwei/p/9151232.html
Copyright © 2020-2023  润新知