【问题标题】:How to get a picture of a place from google maps or places API如何从谷歌地图或地点 API 获取地点的图片
【发布时间】:2012-01-17 17:38:18
【问题描述】:

我正在使用Google Places API 检索有关地点的数据,但找不到如何获取该地点的图片,Google Places API 只是提供了不一样的图标。例如,我需要您在网络浏览器中搜索 Google 地图中的地点时获得的照片。 Panoramio 通常有更多图片,但 Panoramio API 只能按位置搜索图片,而不能按特定餐厅或酒店名称等搜索。有什么想法吗?

【问题讨论】:

    标签: android google-maps google-api google-places-api panoramio


    【解决方案1】:

    我正在创建一个 API,用于在给定 GPS 坐标的情况下获取最佳照片点/位置。有一个免费计划。

    您有不同的端点,例如:

    • 根据 GPS 坐标(纬度和经度),API 会为您提供最近的精选照片位置。
    • 给定 GPS 位置/坐标和半径长度(公里),API 提供该区域中存在的精选照片位置列表(由坐标和半径创建的圆)
    • 给定查询/文本,API 检索与该文本匹配的精选照片位置列表
    • …以及其他一些端点,例如“获取随机点”

    您可以在https://getnofilter.com/developers查看API文档

    新端点即将推出。

    如果您需要延长免费限制,可以给我发电子邮件,我可以做到(查看文档中的电子邮件)

    【讨论】:

      【解决方案2】:
      1. 首先从响应中获取 photo_reference:

         var photoRef;
         if (data.results[i].photos && Array.isArray(data.results[i].photos)) {
         photoRef = data.results[i].photos[0].photo_reference;
         }
        
      2. 使用 Google Photos API 端点获取地点照片/图片:

      https://maps.googleapis.com/maps/api/place/photo?maxwidth=960&photoreference="+ photoRef +"&key=AIzaSyBp0cy7ti0z5MJMAwWiPMNvbJobmWYGyv4' alt=''/>

      1. 自己看看:

      【讨论】:

        【解决方案3】:
        <!DOCTYPE html>
        <html>
          <head>
            <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
            <meta charset="utf-8">
            <title>Places Searchbox</title>
            <style>
              /* Always set the map height explicitly to define the size of the div
               * element that contains the map. */
              #map {
                height: 50%;
              }
              /* Optional: Makes the sample page fill the window. */
              html, body {
                height: 100%;
                margin: 0;
                padding: 0;
              }
              #description {
                font-family: Roboto;
                font-size: 15px;
                font-weight: 300;
              }
        
              #infowindow-content .title {
                font-weight: bold;
              }
        
              #infowindow-content {
                display: none;
              }
        
              #map #infowindow-content {
                display: inline;
              }
        
              .pac-card {
                margin: 10px 10px 0 0;
                border-radius: 2px 0 0 2px;
                box-sizing: border-box;
                -moz-box-sizing: border-box;
                outline: none;
                box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
                background-color: #fff;
                font-family: Roboto;
              }
        
              #pac-container {
                padding-bottom: 12px;
                margin-right: 12px;
              }
        
              .pac-controls {
                display: inline-block;
                padding: 5px 11px;
              }
        
              .pac-controls label {
                font-family: Roboto;
                font-size: 13px;
                font-weight: 300;
              }
        
              #pac-input {
                background-color: #fff;
                font-family: Roboto;
                font-size: 15px;
                font-weight: 300;
                margin-left: 12px;
                padding: 0 11px 0 13px;
                text-overflow: ellipsis;
                width: 400px;
              }
        
              #pac-input:focus {
                border-color: #4d90fe;
              }
        
              #title {
                color: #fff;
                background-color: #4d90fe;
                font-size: 25px;
                font-weight: 500;
                padding: 6px 12px;
              }
              #target {
                width: 345px;
              }
            </style>
          </head>
          <body>
            <input id="pac-input" class="controls" type="text" placeholder="Search Box">
            <div id="map"></div>
            <div id="place-list"></div>
            <script>
              // This example adds a search box to a map, using the Google Place Autocomplete
              // feature. People can enter geographical searches. The search box will return a
              // pick list containing a mix of places and predicted search terms.
        
              // This example requires the Places library. Include the libraries=places
              // parameter when you first load the API. For example:
              // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
        
              function initAutocomplete() {
                var map = new google.maps.Map(document.getElementById('map'), {
                  center: {lat: -33.8688, lng: 151.2195},
                  zoom: 13,
                  mapTypeId: 'roadmap'
                });
        
                // Create the search box and link it to the UI element.
                var input = document.getElementById('pac-input');
                var searchBox = new google.maps.places.SearchBox(input);
                map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
        
                // Bias the SearchBox results towards current map's viewport.
                map.addListener('bounds_changed', function() {
                  searchBox.setBounds(map.getBounds());
                });
        
                var markers = [];
                // Listen for the event fired when the user selects a prediction and retrieve
                // more details for that place.
                searchBox.addListener('places_changed', function() {
                  var places = searchBox.getPlaces();
        
                  if (places.length == 0) {
                    return;
                  }
        
                  // Clear out the old markers.
                  markers.forEach(function(marker) {
                    marker.setMap(null);
                  });
                  markers = [];
        
                  // For each place, get the icon, name and location.
                  var bounds = new google.maps.LatLngBounds();
                  places.forEach(function(place) {
                    if (!place.geometry) {
                      console.log("Returned place contains no geometry");
                      return;
                    }
                    var icon = {
                      url: place.icon,
                      size: new google.maps.Size(71, 71),
                      origin: new google.maps.Point(0, 0),
                      anchor: new google.maps.Point(17, 34),
                      scaledSize: new google.maps.Size(25, 25)
                    };
        
                    var photoUrl = place.photos[0].getUrl({maxWidth: 400, maxHeight: 400});
                    var img = document.createElement("img");
                    img.setAttribute('src', photoUrl + "photo.jpg");
                    document.getElementById('place-list').appendChild(img);
        
                    // Create a marker for each place.
                    markers.push(new google.maps.Marker({
                      map: map,
                      icon: photos[0].getUrl({'maxWidth' : 35,maxHeight' : 35})
                      title: place.name,
                      position: place.geometry.location
                    }));
        
                    if (place.geometry.viewport) {
                      // Only geocodes have viewport.
                      bounds.union(place.geometry.viewport);
                    } else {
                      bounds.extend(place.geometry.location);
                    }
                  });
                  map.fitBounds(bounds);
                });
              }
        
            </script>
            <script src="https://maps.googleapis.com/maps/api/js?key=<KEY_API>&libraries=places&callback=initAutocomplete"
                 async defer></script>
          </body>
          </html>
        

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
        【解决方案4】:

        Places API 现在支持为 Place Search request 返回一张地点照片(如果有),为 Place Details request 返回最多十张地点照片。

        如果您的请求返回了一个照片数组,您可以使用maxheight 和/或maxwidthsensorkey 参数将包含的照片对象中的photo_reference 传递给Place Photo request

        https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRvAAAAwMpdHeWlXl-lH0vp7lez4znKPIWSWvgvZFISdKx45AwJVP1Qp37YOrH7sqHMJ8C-vBDC546decipPHchJhHZL94RcTUfPa1jWzo-rSHaTlbNtjh-N68RkcToUCuY9v2HNpo5mziqkir37WU8FJEqVBIQ4k938TI3e7bf8xq-uwDZcxoUbO_ZJzPxremiQurAYzCTwRhE_V0&sensor=false&key=AddYourOwnKeyHere
        

        更多详情请查看documentation

        【讨论】:

        • 我看到那张照片了。但我想在 JS 中使用它。我可以使用带有 url 的 JSON 格式吗?
        【解决方案5】:

        Places API 刚刚添加了直接在 API 中请求照片的功能:https://developers.google.com/maps/documentation/javascript/places#places_photos

        【讨论】:

          【解决方案6】:

          Places API 将为您提供place detail response 中的坐标(纬度和经度);然后您可以将坐标发送到Panoramio API

          例如(取自 API 文档中的示例):

          https://maps.googleapis.com/maps/api/place/details/json?reference=&lt;big long key for place&gt;&amp;sensor=true&amp;key=AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI

          回应:

          {
            "html_attributions" : [],
            "result" : {
              "address_components" : [
                {
                  "long_name" : "48",
                  "short_name" : "48",
                  "types" : [ "street_number" ]
                },
                {
                  "long_name" : "Pirrama Road",
                  "short_name" : "Pirrama Road",
                  "types" : [ "route" ]
                },
                {
                  "long_name" : "Pyrmont",
                  "short_name" : "Pyrmont",
                  "types" : [ "locality", "political" ]
                },
                {
                  "long_name" : "NSW",
                  "short_name" : "NSW",
                  "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                  "long_name" : "AU",
                  "short_name" : "AU",
                  "types" : [ "country", "political" ]
                },
                {
                  "long_name" : "2009",
                  "short_name" : "2009",
                  "types" : [ "postal_code" ]
                }
              ],
              "formatted_address" : "5/48 Pirrama Road, Pyrmont NSW, Australia",
              "formatted_phone_number" : "(02) 9374 4000",
              "geometry" : {
                "location" : {
                  "lat" : -33.8669710,
                  "lng" : 151.1958750
                }
              },
              "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
              "id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
              "international_phone_number" : "+61 2 9374 4000",
              "name" : "Google Sydney",
              "rating" : 4.60,
              "reference" : "CnRlAAAAAfV6JIqSzL8Cf4VnXn0EaI1d5k3IPhdkEonq0MxiUbQFFSVuptVbXbNH4mrevb0bc7G8yWqTUv76i4KTuO_Wf3OrRHjCJJwzQ0mNLjbYGSVqy2eqyrgOUkl6S_sJfTbHzWZYrfPy7KZaet0mM5S6thIQJYuy5v_JD--ZxXEJLWTQRRoU5UaciXBBo89K-bce18Ii9RsEIws",
              "types" : [ "store", "establishment" ],
              "url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
              "vicinity" : "5/48 Pirrama Road, Pyrmont",
              "website" : "http://www.google.com.au/"
            },
            "status" : "OK"
          }
          

          我们可以看到坐标是 “地点” : { “纬度”:-33.8669710, “液化天然气”:151.1958750 }

          然后我们可以向 Panoramio 发送请求,插入坐标,并在两边加上一点摆动空间(我做了 +/- 0.002 度,赤道处的形状为 200 m x 200 m 正方形,通常更小)。 http://www.panoramio.com/map/get_panoramas.php?set=public&amp;from=0&amp;to=20&amp;minx=-33.868&amp;miny=151.193&amp;maxx=-33.864&amp;maxy=151.197&amp;size=medium&amp;mapfilter=true

          您可能需要对响应进行一些过滤以获得最近的照片,但这应该可以为您提供一些工作。

          【讨论】:

          • 谢谢我在考虑这个,它赢了t necessarily give the picture of the right place but i guess its the closest you can get. Its just weird they have pics on website but dont 无论如何在地图或地点 api 中给他们
          • Panoramio 不再是一个东西。该网站 (panoramio.com) 显示“Panoramio 在 2016 年 11 月 4 日之后不再可用”。 API 请求都以 404 Not Found 响应。
          • 这不再是一个有效的答案。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-12-06
          • 2014-11-18
          • 2012-02-24
          • 1970-01-01
          • 2012-09-26
          • 1970-01-01
          • 2015-10-19
          相关资源
          最近更新 更多