Reputation: 117
I'm creating an app that uses google_places gem and what I want to do is to search hotels and restaurants close to chosen location. Google_places gem helped me a lot, but all it shows is something like:
[#
@place_id="ChIJl6wYnDyG_UYRLHo26ttMYf0", @vicinity="48", @lat=54.1847303, @lng=18.432423, @viewport={"northeast"=>{"lat"=>54.1833813197085, "lng"=>18.1291523802915}, "southwest"=>{"lat"=>54.1833813197085, "lng"=>18.12645441970849}}, @name="Polando no name", @icon="https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
Can I do something to display normally name and e.g. latitude? All I have is:
@types=["restaurant", "food", "point_of_interest", "establishment"],
<%= @client.spots(@trip.latitude, @trip.longitude, :radius => 100, :types => ['hotel','sleep'], detail: true) %
I couldn't find anything helpful in google_places gem repository and google, so I decided to write it here.
Thanks!
Upvotes: 0
Views: 163
Reputation: 4082
There is a function getDetails(...) of Google API that returns more information about some place, extending the one returned by the nearbySearch(...) function.
var SearchPlaces = {
GPMHotels: ['hotel']
, GPMFoodPlaces: ['restaurant']
, GPMShoppingPlaces: ['shopping_mall']
};
var service = new google.maps.places.PlacesService(GPMap);
function SearchGPMapServiceNearPlacesFn(service) {
function GetHTML(place) {
var distance = "";
if (place.geometry && place.geometry.location) {
var fromLatLng = new google.maps.LatLng(GPMapLocation.lat, GPMapLocation.lng);
distance = google.maps.geometry.spherical.computeDistanceBetween(fromLatLng, place.geometry.location);
distance = Math.ceil(distance).toLocaleString('@languaje');
}
var html = "* " + (distance ? distance + " m. " : "") + place.name + ". "
+ place.formatted_address + ". " +
(place.international_phone_number || "");
return html;
}
function GetPlaceDetails(place_id) {
if (!place_id) return;
service.getDetails({
placeId: place_id
}, function (place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
//-->> console.log({ fn: 'GetPlaceDetails', item: place });
$("#GPMNearSitesList").append($('<li>').html(GetHTML(place)));
}
});
}
service.nearbySearch({
location: GPMapLocation,
radius: GPMapOptions.searchRadiusList,
},
function (results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
GetPlaceDetails(results[i].place_id);
//-->>console.log({ fn: "SearchGPMapServiceNearPlacesFn", item: results[i] });
}
}
});
}
See: https://developers.google.com/maps/documentation/javascript/examples/place-details
Upvotes: 0
Reputation: 5363
The code you provided contradicts your title so I'll try to answer both:
In your controller, if you have, @spots = @client.spots(@trip.latitude, @trip.longitude, :radius => 100, :types => ['hotel','sleep'], detail: true)
you should be able to iterate through @spots
in your view.
<% @spots.each do |spot| %>
<%= spot.name %>
<% end %>
When in doubt, hit #methods
(such as spot.methods
) and that will give you some insight as to what you can do with spot
— or any object.
Upvotes: 1