从 google_place gem 获取单个地点的详细信息

Getting single place details from google_place gem

我正在创建一个使用 google_places gem 的应用程序,我想做的是搜索所选位置附近的旅馆和餐馆。 Google_places gem 对我帮助很大,但它显示的只是:

[#

@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",

我可以做些什么来正常显示姓名吗?纬度?我只有:

@types=["restaurant", "food", "point_of_interest", "establishment"],

<%= @client.spots(@trip.latitude, @trip.longitude, :radius => 100, :types => ['hotel','sleep'], detail: true) %

我在 google_places gem 存储库和 google 中找不到任何有用的东西,所以我决定写在这里。

谢谢!

您提供的代码与您的标题相矛盾,所以我会尝试同时回答:

在你的控制器中,如果你有,@spots = @client.spots(@trip.latitude, @trip.longitude, :radius => 100, :types => ['hotel','sleep'], detail: true)

您应该能够在您的视图中遍历 @spots

<% @spots.each do |spot| %>
  <%= spot.name %>
<% end %>

如有疑问,请点击 #methods(例如 spot.methods),这会让您了解可以使用 spot 或任何 [=24= 做什么].

Google API 的函数 getDetails(...) returns 关于某个地方的更多信息,扩展了 nearbySearch(...) 函数返回的信息。

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 = "*&nbsp;" + (distance ? distance + " m. " : "") + place.name + ".&nbsp;&nbsp;"
                       + place.formatted_address + ".&nbsp;&nbsp;" +
            (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] });
            }
        }
    });
}

参见:https://developers.google.com/maps/documentation/javascript/examples/place-details