LBS——地理编码 坐标转地名(C#)

地理编码 高德、百度坐标取地名

  • 坐标取地名
  • http 后台调用方法

采用高德免费Key方式或者百度Key方式 地理编码 (供从经纬度坐标到地址的转换服务)

坐标取地名

        /// 
        /// 坐标转地名类型
        /// 
        public enum CON_TYPE
        {
            [Description("高德坐标转地名")]
            GCJ02_TO_ADDRESS = 1,
            [Description("百度坐标转地名")]
            DB09_TO_ADDRESS = 2
        }
        /// 
        /// 坐标转地名
        /// 如采用高德转地名,请传入标准GCJ02坐标
        /// 如采用百度转地名,请传入标准DB09坐标
        /// 
        /// 纬度(注意坐标系)
        /// 经度(注意坐标系)
        /// 类型
        /// 密钥(高德地名不需要密钥)
        /// 地名
        public static string GPS_TO_ADDRESS(double lat, double lon, CON_TYPE TYPE,string ask=null)
        {
            string MSG = "";
            switch (TYPE)
            {
                case CON_TYPE.GCJ02_TO_ADDRESS:
                    MSG= GCJ02_TO_ADDRESS(lat, lon);
                    break;
                case CON_TYPE.DB09_TO_ADDRESS:
                    MSG = DB09_TO_ADDRESS(lat, lon, ask);
                    break;
            }
            return MSG;
        }

        #region 转化方法
        /// 
        /// 高德转地名
        /// 
        /// 纬度
        /// 经度
        /// 
        private static string GCJ02_TO_ADDRESS(double lat, double lon)
        {
            string url = "http://gc.ditu.aliyun.com/regeocoding";
            Dictionary<string, string> myDic = new Dictionary<string, string>();
            myDic.Add("l", lat + "," + lon);//36.6424,107.6821
            myDic.Add("type", "010");
            string jsonText = "";
            string rusting = "";
            try
            {
                jsonText = CreateHttpGetResponse(url, myDic);
                JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);
                JArray addrList = (JArray)JsonConvert.DeserializeObject(jo["addrList"].ToString());
                string status = addrList[0]["status"].ToString();
                if (status == "1")
                {
                    rusting = addrList[0]["admName"].ToString() + addrList[0]["name"].ToString();
                }
                else
                {
                    rusting= addrList.ToString();
                }
                return rusting;
            }
            catch (Exception)
            {
                return rusting;
            }
        }
        /// 
        /// 百度转地名接口
        /// 
        /// 纬度
        /// 经度
        /// 百度密钥
        /// 地址
        private static string DB09_TO_ADDRESS(double lat, double lon,string ask)
        {
            string url = "http://api.map.baidu.com/geocoder/v2/";
            Dictionary<string, string> myDic = new Dictionary<string, string>();
            myDic.Add("location", lat + "," + lon);
            myDic.Add("coordtype", "gcj02ll");
            myDic.Add("output", "json");
            myDic.Add("ak", ask);
            string jsonText = "";
            string rusting = "无地址";
            try
            {
                jsonText = CreateHttpGetResponse(url, myDic);
                JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);
                if (jo["status"].ToString() == "0")
                {
                    JObject joadd = (JObject)JsonConvert.DeserializeObject(jo["result"].ToString());
                    rusting = joadd["formatted_address"].ToString();
                }
                else if (jo["status"].ToString() == "4")
                {
                    rusting = "ASK超额";
                }
                else
                {
                    rusting = jo.ToString();
                }
                return rusting;
            }
            catch (Exception)
            {
                return rusting;
            }
        }
        #endregion

http 后台调用方法

        /// 
        /// 后台http调用
        /// 
        /// ip地址
        /// 参数
        /// 返回结果
       public static string CreateHttpGetResponse(string url, IDictionary<string, string> parameters)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            HttpWebRequest request = null;

            StringBuilder buffer = new StringBuilder();
            if (!(parameters == null || parameters.Count == 0))
            {
                int i = 0;
                foreach (string key in parameters.Keys)
                {
                    if (i > 0)
                    {
                        buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                    }
                    else
                    {
                        buffer.AppendFormat("?{0}={1}", key, parameters[key]);
                    }
                    i++;
                }
            }
            //如果是发送HTTPS请求   
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                //request = WebRequest.Create(url) as HttpWebRequest;
                //request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = WebRequest.Create(url + buffer.ToString()) as HttpWebRequest;
            }
            request.Method = "GET";

            request.Headers.Add("X_REG_CODE", "288a633ccc1");
            request.Headers.Add("X_MACHINE_ID", "a306b7c51254cfc5e22c7ac0702cdf87");
            request.Headers.Add("X_REG_SECRET", "de308301cf381bd4a37a184854035475d4c64946");
            request.Headers.Add("X_STORE", "0001");
            request.Headers.Add("X_BAY", "0001-01");
            request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Accept-Language", "zh-CN");
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Accept = "*/*";

            request.CookieContainer = new CookieContainer();


            HttpWebResponse res;
            try
            {
                res = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                res = (HttpWebResponse)ex.Response;
            }
            Stream s = res.GetResponseStream();
            StreamReader sr = new StreamReader(s);

            //读取服务器端返回的消息 
            string sReturnString = sr.ReadLine();
            return sReturnString;
        }

你可能感兴趣的:(LBS,与地图有关的计算)