Get list of nearby places from Google Places API (Swift 3)

ε祈祈猫儿з 提交于 2019-11-30 14:19:24

Lightweight Solution!

I created a google wrapper to call google near by api here : Google Api Helper

var input = GInput()
input.keyword = "Restaurants"
input.radius = 20000
var location = GLocation()
location.latitude = 26.273178
location.longitude = 73.009545
input.destinationCoordinate = location
GoogleApi.shared.callApi(.nearBy, input: input) { (response) in
    if let data = response.data as? [GApiResponse.NearBy], response.isValidFor(.nearBy){
        // all nearby places
    }
}

Interesting part is I added a bonus api to get all the 60 nearby places so the user don't have to worry about the next page token and call the api till the final result come. Here is an example to get all the result.

var input = GInput()
input.keyword = "Restaurants"
input.radius = 20000
var location = GLocation()
location.latitude = 26.273178
location.longitude = 73.009545
input.destinationCoordinate = location
NearbyExtension.shared.completion = { response in
    if let data = response.data as? [GApiResponse.NearBy], response.isValidFor(.nearBy){
        // all nearby places
    }
}
NearbyExtension.shared.getAllNearBy(input: input)

Here is an example url, where it returns a list of starbucks near your location.

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=(yourlatitude),(yourlongitude)&radius=5000&keyword=starbucks&key=(yourkey)

You can get the key from google console

Check this out -->> https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial

"Finding Something to Eat" section have what you needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!