zoukankan      html  css  js  c++  java
  • C# 根据根据经纬度获取地址(使用高德API)

    因为需求只要完整地址所以用字符串解析。并没有使用Json解析。

      public class AmapUtil
        {
            const string key = "132976335b2e02f3d6455f91bb8a98d4";
    
            /// <summary>
            /// 根据经纬度获取地址
            /// </summary>
            /// <param name="LngLatStr">经度纬度组成的字符串 例如:"113.692100,34.752853"</param>
            /// <param name="timeout">超时时间默认10秒</param>
            /// <returns>失败返回"" </returns>
            public static string GetLocationByLngLat(string LngLatStr, int timeout = 10000)
            {
                string url = $"http://restapi.amap.com/v3/geocode/regeo?key={key}&location={LngLatStr}";
                return GetLocationByURL(url, timeout);
            }
    
            /// <summary>
            /// 根据经纬度获取地址
            /// </summary>
            /// <param name="lng">经度 例如:113.692100</param>
            /// <param name="lat">维度 例如:34.752853</param>
            /// <param name="timeout">超时时间默认10秒</param>
            /// <returns>失败返回"" </returns>
            public static string GetLocationByLngLat(double lng,double lat, int timeout = 10000)
            {
                string url = $"http://restapi.amap.com/v3/geocode/regeo?key={key}&location={lng},{lat}";
                return GetLocationByURL(url, timeout);
            }
            /// <summary>
            /// 根据URL获取地址
            /// </summary>
            /// <param name="url">Get方法的URL</param>
            /// <param name="timeout">超时时间默认10秒</param>
            /// <returns></returns>
            private static string GetLocationByURL(string url,int timeout=10000)
            {
                string strResult = "";
                try
                {
                    HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                    req.ContentType = "multipart/form-data";
                    req.Accept = "*/*";
                    //req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
                    req.UserAgent = "";
                    req.Timeout = timeout;
                    req.Method = "GET";
                    req.KeepAlive = true;
                    HttpWebResponse response = req.GetResponse() as HttpWebResponse;
                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        strResult = sr.ReadToEnd();
                    }
                    int formatted_addressIndex = strResult.IndexOf("formatted_address");
                    int addressComponentIndex = strResult.IndexOf("addressComponent");
                    int cutIndex = addressComponentIndex - formatted_addressIndex - 23;
                    int subIndex = formatted_addressIndex + 20;
                    return strResult.Substring(subIndex, cutIndex);
                }
                catch (Exception)
                {
                    strResult = "";
                }
                return strResult;
            }
        }
    

      

  • 相关阅读:
    Javascript数组与字符串常用api
    javaScript入门(函数)
    javascript入门(1)
    微信小程序组件解读和分析:一、view(视图容器 )
    微信小程序组件解读和分析:十五、switch 开关选择器
    微信小程序组件解读和分析:十四、slider滑动选择器
    微信小程序组件解读和分析:十三、radio单选项目
    微信小程序组件解读和分析:十二、picker滚动选择器
    微信小程序组件解读和分析:十一、label标签
    微信小程序组件解读和分析:十、input输入框
  • 原文地址:https://www.cnblogs.com/HandLoong/p/8511010.html
Copyright © 2011-2022 走看看