DBA

REST API Call : Kakao API Address :https://dapi.kakao.com/v2/local/search/address.json 본문

[3] Development/Swift

REST API Call : Kakao API Address :https://dapi.kakao.com/v2/local/search/address.json

코볼 2023. 3. 23. 14:59
728x90
반응형
SMALL

Alamofire Version :

Installing Alamofire (5.6.4)

 

 

 

 

import Alamofire

 

func getAddress() -> Void {

         let headers: HTTPHeaders = ["Authorization": "KakaoAK 발급받은 키"]

        let parameters: Parameters = ["query": "서울특별시 강남구 역삼동"]

        let url = "https://dapi.kakao.com/v2/local/search/address.json"

 

        // 구조체 선언

        struct SearchResult: Codable {

            let documents: [Document]

            let meta: Meta

        }

 

        struct Document: Codable {

            let address: Address

            let addressName: String

            let addressType: String

            let roadAddress: String?

            let x: String

            let y: String

            

            enum CodingKeys: String, CodingKey {

                case address

                case addressName = "address_name"

                case addressType = "address_type"

                case roadAddress = "road_address_name"

                case x

                case y

            }

        }

 

        struct Address: Codable {

            let addressName: String

            let bCode: String

            let hCode: String

            let mainAddressNo: String

            let mountainYN: String

            let region1DepthName: String

            let region2DepthName: String

            let region3DepthHName: String

            let region3DepthName: String

            let subAddressNo: String

            let x: String

            let y: String

            

            enum CodingKeys: String, CodingKey {

                case addressName = "address_name"

                case bCode = "b_code"

                case hCode = "h_code"

                case mainAddressNo = "main_address_no"

                case mountainYN = "mountain_yn"

                case region1DepthName = "region_1depth_name"

                case region2DepthName = "region_2depth_name"

                case region3DepthHName = "region_3depth_h_name"

                case region3DepthName = "region_3depth_name"

                case subAddressNo = "sub_address_no"

                case x

                case y

            }

        }

 

        struct Meta: Codable {

            let isEnd: Bool

            let pageableCount: Int

            let totalCount: Int

            

            enum CodingKeys: String, CodingKey {

                case isEnd = "is_end"

                case pageableCount = "pageable_count"

                case totalCount = "total_count"

            }

        }

 

        AF.request(url, method: .get, parameters: parameters, headers: headers).responseDecodable(of: SearchResult.self) { response in

                switch response.result {

                case .success(let result):

                    print(result)

                    let address = result.documents[0].address.addressName

                    print(address)

                case .failure(let error):

                    print(error.localizedDescription)

                }

            }

 

}

 

 

위 함수 호출 결과

SearchResult(documents: [CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49d10).Document(address: CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49d98).Address(addressName: "서울 강남구 역삼동", bCode: "1168010100", hCode: "", mainAddressNo: "", mountainYN: "N", region1DepthName: "서울", region2DepthName: "강남구", region3DepthHName: "", region3DepthName: "역삼동", subAddressNo: "", x: "127.03306536185", y: "37.4953666908089"), addressName: "서울 강남구 역삼동", addressType: "REGION", roadAddress: nil, x: "127.03306536185", y: "37.4953666908089"), CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49d10).Document(address: CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49d98).Address(addressName: "서울 강남구 역삼1동", bCode: "", hCode: "1168064000", mainAddressNo: "", mountainYN: "N", region1DepthName: "서울", region2DepthName: "강남구", region3DepthHName: "역삼1동", region3DepthName: "", subAddressNo: "", x: "127.033201083326", y: "37.4954171091244"), addressName: "서울 강남구 역삼1동", addressType: "REGION", roadAddress: nil, x: "127.033201083326", y: "37.4954171091244"), CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49d10).Document(address: CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49d98).Address(addressName: "서울 강남구 역삼2동", bCode: "", hCode: "1168065000", mainAddressNo: "", mountainYN: "N", region1DepthName: "서울", region2DepthName: "강남구", region3DepthHName: "역삼2동", region3DepthName: "", subAddressNo: "", x: "127.046839198832", y: "37.4959639052057"), addressName: "서울 강남구 역삼2동", addressType: "REGION", roadAddress: nil, x: "127.046839198832", y: "37.4959639052057")], meta: CocoapodsTest.ViewController.(unknown context at $102c49c24).(unknown context at $102c49e2c).Meta(isEnd: true, pageableCount: 3, totalCount: 3))

서울 강남구 역삼동

728x90
반응형
LIST
Comments