새소식

개발일지

TripMate (2) - 주변 지역 검색 API

  • -

이전 포스팅

 

TripMate (1) - openweatherAPI와 CLLocationManager 사용하기

새로 시작하는 프로젝트에서 날씨를 가져오는 API를 사용하게 되었다. 예전 패스트캠퍼스 강의에서 날씨 앱을 만들 때 사용했던 openweather API를 사용했다. Сurrent weather and forecast - OpenWeatherMap Access

kimkhuna99.tistory.com


이전에 사용하던 GoogleMap API를 사용하려다가 주변 지역을 검색해 주는 PlacesAPI가 더욱 앱의 취지에 맞는 것 같아 바꿨다.

 

주변 지역 검색  |  Places API  |  Google for Developers

이제 Places API (신규)가 출시되면서 차세대 Places API를 사용할 수 있습니다. 이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 의견 보내기 주변 지역 검색 컬렉션을 사용해 정리

developers.google.com

 

데이터를 요청하는 URL은 아래와 같이 사용하였다.

https://maps.googleapis.com/maps/api/place/nearbysearch/json
?keyword=restaurant // 음식점
&location= 위도, 경도 // 위치
&radius=1500 // 반경
&key={API Key}

 

 

나는 데이터를 불러오기 위한 구조체를 아래와 같이 선언하였다.

struct Place: Decodable{
    let results: [PlaceDetail]
}

struct PlaceDetail: Decodable{
    let isOpen: String		// 영업 유무
    let name: String    	// 가게 이름
    let geometry: Geometry 	// 경도, 위도
    let rating: Double		// 별점
    
    enum CodingKeys: String, CodingKey {
        case isOpen = "business_status"
        case name
        case geometry
        case rating
    }
}

struct Geometry: Decodable{
    let location: Location
}

struct Location: Decodable{
    let lat: Double	// 위도
    let lng: Double	// 경도
}

 

API 문서에 더 자세하게 나오지만 다른 여러가지의 기능을 사용할 수 있다.

 

여행 앱을 만들다보니 앱에서 Google Map을 사용하지 않을 수 없었다.

이번을 마지막으로 Google Map API는 작업이 완료되었다.

728x90
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.