如何从谷歌地图或地方API获取一个地方的图片

时间:2011-12-11 06:00:28

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

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

6 个答案:

答案 0 :(得分:10)

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

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

https://maps.googleapis.com/maps/api/place/details/json?reference=<big long key for place>&sensor=true&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"
}

我们可以看到坐标是           “地点” : {             “lat”: - 33.8669710,             “lng”:151.1958750           }

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

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

答案 1 :(得分:10)

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

答案 2 :(得分:8)

Places API现在支持返回一张地方照片(如果适用于Place Search request)和最多十张地方照片Place Details request

如果您的请求返回了照片数组,则可以将photo_reference从包含的照片对象传递到maxheight和/或maxwidth的{​​{3}}, sensorkey参数:

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

有关详细信息,请参阅Place Photo request

答案 3 :(得分:1)

<!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>

答案 4 :(得分:1)

  1. 首先从响应中获取照片参考:

     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. 亲自看看:

enter image description here

答案 5 :(得分:0)

我正在创建一个 API,用于根据 GPS 坐标获取最佳照片点/位置。有一个免费计划。

您有不同的端点,例如:

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

您可以在 https://getnofilter.com/developers

中查看 API 文档

新端点即将推出。

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