【问题标题】:Getting single place details from google_place gem从 google_place gem 获取单个地点的详细信息
【发布时间】:2018-06-13 14:45:26
【问题描述】:

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

[#

@place_id="ChIJl6wYnDyG_UYRLHo26ttMYf0", @vicinity="48", @lat=54.1847303,@lng=18.432423, @viewport={"东北"=>{"纬度"=>54.1833813197085, "lng"=>18.1291523802915}, "西南"=>{"lat"=>54.1833813197085, "lng"=>18.12645441970849}}, @name="波兰无名", @icon="https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",

我可以做些什么来正常显示名称和例如纬度?我只有:

@types=["restaurant", "food", "point_of_interest", "机构"],

100, :types => ['hotel','sleep'], detail: true) %

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

谢谢!

【问题讨论】:

    标签: ruby-on-rails rubygems google-places


    【解决方案1】:

    您提供的代码与您的标题相矛盾,因此我将尝试同时回答:

    在您的控制器中,如果有,@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 或任何对象做什么。

    【讨论】:

      【解决方案2】:

      Google APIgetDetails(...) 函数可以返回有关某个地方的更多信息,扩展了 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多