본문 바로가기

Web_Application/C#

[C#][다음API] 좌표로 주소가져오기



다음 API : 좌표로 주소 얻어내기

        #region " [ gps 좌표로 주소(행정동) 반환 ] "

        public class ToAddress
        {
            public string type { get; set; }
            public string code { get; set; }
            public string name { get; set; }
            public string fullName { get; set; }
            public string regionId { get; set; }
            public string name0 { get; set; }
            public string code1 { get; set; }
            public string name1 { get; set; }
            public string code2 { get; set; }
            public string name2 { get; set; }
            public string code3 { get; set; }
            public string name3 { get; set; }
            public float x { get; set; }
            public float y { get; set; }
        }

        public Coordinate.ToAddress GetCoordinates2BupAddress(string lng, string lat)
        {
            WebResponse response;
            StreamReader reader;
            Coordinate.ToAddress coordinate = null;

            using (var client = new HttpClient())
            {
                try
                {
                    var ServiceUrl = String.Format("https://apis.daum.net/local/geo/coord2addr?apikey={{API_KEY}}&longitude={0}&latitude={1}&inputCoordSystem=WGS84&output=json", lng, lat);

                    WebRequest request = WebRequest.Create(ServiceUrl);
                    // If required by the server, set the credentials.
                    request.Credentials = CredentialCache.DefaultCredentials;
                    // Get the response.
                    response = request.GetResponse();
                    // Display the status.
                    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                    // Get the stream containing content returned by the server.
                    Stream dataStream = response.GetResponseStream();
                    // Open the stream using a StreamReader for easy access.
                    reader = new StreamReader(dataStream);
                    // Read the content.
                    string responseFromServer = reader.ReadToEnd();

                    //Console.WriteLine(responseFromServer);

                    coordinate = JsonConvert.DeserializeObject<Coordinate.ToAddress>(responseFromServer);
                }
                catch (Exception e)
                {
                    Console.WriteLine("in api 좌표 변환 실패 : " + lng + "|" + lat);
                }
            }

            return coordinate;
        }

        #endregion