qpowell / google_places

A Ruby wrapper around the Google Places API
MIT License
382 stars 188 forks source link

Prediction#list_by_input is more flexible, plus distance_meters #134

Open drnic opened 4 years ago

drnic commented 4 years ago

Prediction#list_by_input is more flexible (allows any options); and supports distance_meters field if origin parameter provided

https://developers.google.com/places/web-service/autocomplete#place_autocomplete_results

With distance_meters, you can sort the predictions by closeness to the origin:

# pp client.predictions_by_input("wal", origin: "40.730610,-73.935242", location: "40.730610,-73.935242").sort {|p1, p2| p1.distance_meters.to_i <=> p2.distance_meters.to_i}.map {|p| [p.description, p.distance_meters]}
[["Wall Street, New York, NY, USA", nil],
 ["Waldorf Astoria, Park Avenue, New York, NY, USA", 4262],
 ["Walmart Supercenter, Park Plaza Drive, Secaucus, NJ, USA", 11376],
 ["Walmart Supercenter, 88th Street, North Bergen, NJ, USA", 11379],
 ["Wallington, NJ, USA", 20286]]

The .distance_meters.to_i was necessary because the "Wall Street" entry was distance_meters == nil 🤷