๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/Third Party

[iOS/API] ๋„ค์ด๋ฒ„ API๋ฅผ ์ด์šฉํ•ด์„œ ์ฃผ์†Œ๋ฅผ ์œ„๋„,๊ฒฝ๋„๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ(NAVER CLOUD PLATFORM GEOCODING)

by Fomagran ๐Ÿ’ป 2021. 3. 23.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ์— ์žˆ๋Š” Geocoding ์„ ์ด์šฉํ•ด์„œ ์›ํ•˜๋Š” ์ฃผ์†Œ์˜ ์œ„๋„,๊ฒฝ๋„๊ฐ’์„ ๋ฐ›์•„์˜ฌ๊ฑฐ์—์š”!

 

์• ํ”Œ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ง€์˜ค์ฝ”๋”ฉ๋„ ์žˆ์ง€๋งŒ ํ•œ๊ตญ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ๊ฝค ๋งŽ์ด ๋‚˜์˜ค๋”๋ผ๊ตฌ์š”.

 

๊ตฌ๊ธ€ API๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋‚˜...? ๊ณ ๋ฏผํ•˜๋Š”๋ฐ ๊ตฌ๊ธ€์€ 2๋งŒํšŒ๊นŒ์ง€ ๋ฌด๋ฃŒ๊ณ  ๊ทธ ์ด์ƒ๋˜๋ฉด ์œ ๋ฃŒ๋กœ ์ง€๋ถˆํ•ด์•ผ ๋˜๋”๋ผ๊ตฌ์š”.

 

ํ˜น์‹œ๋‚˜ ํ•˜๊ณ  ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ ์„ ๋“ค์–ด๊ฐ€๋ดค๋”๋‹ˆ... 300๋งŒํšŒ๊นŒ์ง€ ๋ฌด๋ฃŒ....๐Ÿ˜

 

๊ณ ๋ฏผํ• ๊ฑฐ ์—†์ด ๋ฐ”๋กœ ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ ์„ ์ด์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~!!

 


NAVER CLOUD PLATFORM

 

๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ์œผ๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”!

 

NAVER CLOUD PLATFORM

cloud computing services for corporations, IaaS, PaaS, SaaS, with Global region and Security Technology Certification

www.ncloud.com

์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ๋ณด์‹œ๋ฉด ๋กœ๊ทธ์ธ์ด ์žˆ์„๊ฑฐ์—์š”.(์ €๋Š” ๋กœ๊ทธ์ธํ•œ ์ƒํƒœ๋ผ ๋กœ๊ทธ์•„์›ƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.)

 

์—ฌ๊ธฐ์„œ ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ ์ „์šฉ ์•„์ด๋””๋ฅผ ํšŒ์›๊ฐ€์ž… ํ•ด์ค๋‹ˆ๋‹ค.

 

 

ํšŒ์›๊ฐ€์ž…์„ ๋งˆ์น˜์…จ์œผ๋ฉด ๋กœ๊ทธ์•„์›ƒ ๋ฐ”๋กœ ์•„๋ž˜์ชฝ์— Console ์ด๋ผ๊ณ  ์žˆ๋Š”๋ฐ์š”. ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด์„œ ๋“ค์–ด๊ฐ€์ฃผ์„ธ์š”.

 

๋“ค์–ด๊ฐ€์‹œ๋ฉด ์™ผ์ชฝ์— AI-NAVER API๋ฅผ ํด๋ฆญํ•˜์‹œ๋ฉด Application์ด ๋‚˜์˜ฌํ…๋ฐ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”!

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋‚˜์˜ฌํ…๋ฐ + Application ๋“ฑ๋ก์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”!

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ •๋ง ๋งŽ์€ API๊ฐ€ ์žˆ์–ด์š”.

 

์ด ์ค‘์—์„œ ์šฐ๋ฆฌ๋Š” Maps์— Geocoding์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์— iOS Bundle์— ํ”„๋กœ์ ํŠธ ๋ฒˆ๋“ค์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ์„ธ์š”! (+์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์…”์•ผ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.)

๊ทธ๋ฆฌ๊ณ  ๋“ฑ๋ก๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด Geocoding ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•ฑ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

์œ„ ์‚ฌ์ง„์—์„œ App ์ด๋ฆ„ ์•„๋ž˜ ์ธ์ฆ ์ •๋ณด๋ผ๊ณ  ์žˆ๋Š”๋ฐ์š”.

 

์ด๊ฑธ ํด๋ฆญํ•˜๋ฉด Client ID์™€ Client Secret์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. 

 

API ์ด์šฉ์— ํ•„์š”ํ•˜๋‹ˆ ๋ฐ˜๋“œ์‹œ ์–ด๋””์— ์ €์žฅํ•ด๋‘์„ธ์š”!

์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ geocode ์„ค๋ช…์„œ๋ฅผ ๋ณด๋ฉด ์š”์ฒญ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ๋‹ค๊ณ  ๋‚˜์™€์žˆ์–ด์š”.

 

์ €ํ•œํ…Œ๋Š” ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ๋ฌด์Šจ ๋ง์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋”๋ผ๊ตฌ์š”.... ์•„๊นŒ ์ €์žฅํ•ด๋†“์€ ํด๋ผ์ด์–ธํŠธ ์•„์ด๋””ํ•˜๊ณ  ์‹œํฌ๋ฆฟ์ด ํ•„์š”ํ•œ๊ฑฐ ๊ฐ™์€๋ฐ...

 

-data-urlencode ๋Š” ๋ญ๊ณ ... -H๋Š” ๋ญ์•ผ... ์–ด๋–ค์‹์œผ๋กœ ๋„ฃ์–ด์•ผํ•ด....

 

curl -G "https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode" \
    --data-urlencode "query={์ฃผ์†Œ}" \
    --data-urlencode "coordinate={๊ฒ€์ƒ‰_์ค‘์‹ฌ_์ขŒํ‘œ}" \
    -H "X-NCP-APIGW-API-KEY-ID: {์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก ์‹œ ๋ฐœ๊ธ‰๋ฐ›์€ client id๊ฐ’}" \
    -H "X-NCP-APIGW-API-KEY: {์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋“ฑ๋ก ์‹œ ๋ฐœ๊ธ‰๋ฐ›์€ client secret๊ฐ’}" -v

 

๋ญ ์ด๋Ÿฐ์‹์œผ๋กœ ์š”์ฒญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด JSON ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋„ค์š”!

 

{
    "status": "OK",
    "meta": {
        "totalCount": 1,
        "page": 1,
        "count": 1
    },
    "addresses": [
        {
            "roadAddress": "๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ ๋ถ„๋‹น๊ตฌ ๋ถˆ์ •๋กœ 6 ๊ทธ๋ฆฐํŒฉํ† ๋ฆฌ",
            "jibunAddress": "๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ ๋ถ„๋‹น๊ตฌ ์ •์ž๋™ 178-1 ๊ทธ๋ฆฐํŒฉํ† ๋ฆฌ",
            "englishAddress": "6, Buljeong-ro, Bundang-gu, Seongnam-si, Gyeonggi-do, Republic of Korea",
            "addressElements": [
                {
                    "types": [
                        "POSTAL_CODE"
                    ],
                    "longName": "13561",
                    "shortName": "",
                    "code": ""
                }
            ],
            "x": "127.10522081658463",
            "y": "37.35951219616309",
            "distance": 20.925857741585514
        }
    ],
    "errorMessage": ""
}

 

 

๊ตฌ์ฒด์ ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ์‚ดํŽด๋ณด๊ณ  ์‹ถ์œผ์‹  ๋ถ„๋“ค์€

 

์•„๋ž˜ geocode ์„ค๋ช…์„œ๋กœ ์ด๋™ํ•˜์…”์„œ ํ™•์ธํ•ด์ฃผ์„ธ์š”!

 

 

geocode - API ์ฐธ์กฐ์„œ

๊ฐœ์š” ์ฃผ์†Œ ๊ฒ€์ƒ‰ API๋Š” ์ง€๋ฒˆ, ๋„๋กœ๋ช…๋ฅผ ์งˆ์˜์–ด๋กœ ์‚ฌ์šฉํ•ด์„œ ์ฃผ์†Œ ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋กœ ์ฃผ์†Œ ๋ชฉ๋ก๊ณผ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ JSON ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์š”์ฒญ curl -G "https://naveropenapi.apigw.ntruss.com/map-geocod

apidocs.ncloud.com


Xcode

 

์ž ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ํ”„๋กœ์ ํŠธ๋กœ ์ด๋™ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์„ธํŒ…ํ•ด๋ด…์‹œ๋‹ค!

 

๋จผ์ € ์ €๋Š” ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํ•˜๊ธฐ ์œ„ํ•ด Alamofire๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ JSON์„ ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•ด SwiftyJSON์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 

podํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”! (๋ฒ„์ „์€ ๋‹ค๋ฅผ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ํ™•์ธํ•ด์ฃผ์„ธ์š”)

pod 'Alamofire', '~> 5.2'
  
pod 'SwiftyJSON', '~> 4.0'

ViewController

 

๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ด๋™ํ•ด์„œ ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ import ํ•ด์ค๋‹ˆ๋‹ค.

import Alamofire
import SwiftyJSON

 

๊ฐ€์žฅ ๋จผ์ € ๋„ค์ด๋ฒ„ API ๋ณ€์ˆ˜๋“ค์„ ์„ธํŒ…ํ•ด์ค„๊ฑฐ์—์š”.

 

์œ„์—์„œ ์ €์žฅํ•ด์ค€ ๋„ค์ด๋ฒ„ ํด๋ผ์ด์–ธํŠธ ์•„์ด๋””์™€ ์‹œํฌ๋ฆฟ ๊ทธ๋ฆฌ๊ณ  ์ง€์˜ค์ฝ”๋“œ url์„ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

   let NAVER_CLIENT_ID = "๋„ค์ด๋ฒ„ ํด๋ผ์ด์–ธํŠธ ์•„์ด๋””"
   let NAVER_CLIENT_SECRET = "๋„ค์ด๋ฒ„ ํด๋ผ์ด์–ธํŠธ ์‹œํฌ๋ฆฟ"
   let NAVER_GEOCODE_URL = "https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query="

 

๊ทธ๋ฆฌ๊ณ  ์›ํ•˜๋Š” ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ทธ ์ฃผ์†Œ๋ฅผ encodingํ•œ ์ฃผ์†Œ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 let ํ™๋Œ€์ž…๊ตฌ์—ญ = "์„œ์šธ์‹œ ๋งˆํฌ๊ตฌ ๋™๊ต๋™"
 let encodeAddress = ํ™๋Œ€์ž…๊ตฌ์—ญ.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

 

๋„ค์ด๋ฒ„ ํด๋ผ์ด์–ธํŠธ ์•„์ด๋””์™€ ๋„ค์ด๋ฒ„ ํด๋ผ์ด์–ธํŠธ ์‹œํฌ๋ฆฟ์„ HTTPHeader๋กœ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด์ฃผ๊ณ 

 

HTTPHeaders์•ˆ์— ๋‘ ํ—ค๋”๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋„ฃ์–ด์ฃผ์„ธ์š”.

 

   override func viewDidLoad() {
        super.viewDidLoad()
        
        let header1 = HTTPHeader(name: "X-NCP-APIGW-API-KEY-ID", value: NAVER_CLIENT_ID)
        let header2 = HTTPHeader(name: "X-NCP-APIGW-API-KEY", value: NAVER_CLIENT_SECRET)
        let headers = HTTPHeaders([header1,header2]) 
        ...

๊ทธ ์•„๋ž˜์— Alamofire๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋„ค์ด๋ฒ„ ์ง€์˜ค์ฝ”๋“œ url์— encodeํ•œ ์ฃผ์†Œ๋ฅผ ํ•ฉ์ณ์„œ ๋„ฃ์–ด์ฃผ๊ณ  method ๋Š” GET๋ฐฉ์‹์œผ๋กœ

 

headers์— ์œ„์—์„œ ๋งŒ๋“ค์–ด์ค€ headers๋ฅผ ๋„ฃ์–ด์ค€ ๋’ค JSON ๋ฐฉ์‹์œผ๋กœ ์‘๋‹ต ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

 

๊ฒฐ๊ณผ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์™”๋‹ค๋ฉด JSON์œผ๋กœ ์˜จ ๊ฒฐ๊ณผ๋ฅผ SwiftyJSON ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

์œ„ JSON ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด addresses ์˜ ํฐ ๋ฌถ์Œ ์•ˆ์— ์œ„๋„๋Š” x๋กœ ๊ฒฝ๋„๋Š” y๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ณ ๋กœ data๋ฅผ json["addresses"]๋กœ ๋„ฃ์–ด์ค€ ๋’ค y๋ฅผ ์œ„๋„, x๋ฅผ ๊ฒฝ๋„๋กœ ๊ฐ๊ฐ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

    AF.request(NAVER_GEOCODE_URL + encodeAddress, method: .get,headers: headers).validate()
        .responseJSON { response in
            switch response.result {
            case .success(let value as [String:Any]):
                let json = JSON(value)
                let data = json["addresses"]
                let lat = data[0]["y"]
                let lon = data[0]["x"]
                print("ํ™๋Œ€์ž…๊ตฌ์—ญ์˜","์œ„๋„๋Š”",lat,"๊ฒฝ๋„๋Š”",lon)
            case .failure(let error):
                print(error.errorDescription ?? "")
            default :
                fatalError()
            }
        }

 

์ถœ๋ ฅํ•œ ๊ฒƒ์„ ํ™•์ธํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž˜ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

ํ•œ๋ฒˆ ์ œ๋Œ€๋กœ ๋‚˜์™”๋Š”์ง€ ํ™•์ธํ•ด๋ด์•ผ๊ฒ ์ฃ ?


Google Map

 

๋จผ์ € ๊ตฌ๊ธ€๋งต์œผ๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”!

 

 

Google ์ง€๋„

Google ์ง€๋„์—์„œ ์ง€์—ญ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ง€๋„๋ฅผ ์‚ดํŽด๋ณด๊ฑฐ๋‚˜ ์šด์ „๊ฒฝ๋กœ ์ •๋ณด๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

www.google.co.kr

์œ„์—์„œ ๋‚˜์˜จ ์œ„๋„์™€ ๊ฒฝ๋„๋ฅผ ๊ฒ€์ƒ‰ํ•ด์ฃผ๋ฉด

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ •ํ™•ํžˆ ํ™๋Œ€์ž…๊ตฌ์—ญ์€ ์•„๋‹ˆ์ง€๋งŒ (์„œ์šธ์‹œ ๋งˆํฌ๊ตฌ ๋™๊ต๋™์ด๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•ด์„œ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค.)

 

ํ™๋Œ€์ž…๊ตฌ์—ญ ๊ทผ์ฒ˜ ์ขŒํ‘œ๊ฐ€ ์ฐํžˆ๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค !!


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ์„ ํ™œ์šฉํ•ด์„œ ์ฃผ์†Œ์˜ ์œ„๋„ ๊ฒฝ๋„๋ฅผ ์•Œ์•„์˜ค๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ๊ฑฐ๋‚˜ ์ง€์ ํ•ด์ฃผ์‹ค ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€