迁移到新的“地点照片”
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Place Photos,您可以向网页添加高品质照片内容。本页面介绍了 Place
类(新)和 PlacesService
(旧)中的地点照片功能之间的差异,并提供了一些代码段以供比较。
PlacesService
(旧版)对于任何 getDetails()
请求,如果请求中指定了 photos
字段,则会返回一个最多包含 10 个 PlacePhoto
对象的数组,作为 PlaceResult
对象的一部分。对于 textSearch()
和 nearbySearch()
,如果存在第一张地点照片,则默认返回该照片。
- 如果请求中指定了
photos
字段,Place
类会返回一个最多包含 10 个 Photo
对象的数组,作为 fetchFields()
请求的一部分。
下表列出了 Place
类和 PlacesService
在使用地点照片方面的一些主要区别:
代码比较
本部分将比较地点照片的代码,以说明 Places 服务与 Place 类之间的区别。以下代码段展示了在每个相应 API 上请求地点照片所需的代码。
地点服务(旧版)
以下代码段展示了如何使用 PlacesService
返回照片,以及如何在网页上显示第一个照片结果。在此示例中,地点详情请求指定了一个地点 ID,以及 name
和 photos
字段。然后,在检查服务状态后,页面上会显示第一张照片。实例化 PlacesService
时,必须指定地图或 div
元素;由于此示例不包含地图,因此使用 div
元素。
function getPhotos() {
// Construct the Place Details request.
const request = {
placeId: "ChIJydSuSkkUkFQRsqhB-cEtYnw",
fields: ["name", "photos"],
};
// Create an instance of PlacesService.
const attributionDiv = document.getElementById("attribution-div");
const service = new google.maps.places.PlacesService(attributionDiv);
// Check status and display the first photo in an img element.
service.getDetails(request, (place, status) => {
if (
status === google.maps.places.PlacesServiceStatus.OK && place
) {
const photoImg = document.getElementById('image-container');
photoImg.src = place.photos[0].getUrl({maxHeight: 400});
}
});
}
PlacesService
中的作者署名
PlacesService
会以 html_attributions
字符串的形式返回所需的作者署名,其中包含指向作者 Google 个人资料页面的网址。以下代码段展示了如何检索第一个照片结果的提供方信息数据。
let attributionUrl = place.photos[0].html_attributions;
了解详情
地点类(新)
以下代码段展示了如何使用 fetchFields()
方法返回地点详细信息,包括显示名称和地点照片。首先,使用地点 ID 实例化一个新的 Place
对象,然后调用 fetchFields()
,其中指定了 displayName
和 photos
字段。
然后,页面上会显示获得第一名的照片。使用 Place
类时,无需检查服务状态,因为系统会自动处理。
async function getPhotos() {
// Use a place ID to create a new Place instance.
const place = new google.maps.places.Place({
id: 'ChIJydSuSkkUkFQRsqhB-cEtYnw', // Woodland Park Zoo, Seattle WA
});
// Call fetchFields, passing the needed data fields.
await place.fetchFields({ fields: ['displayName','photos'] });
console.log(place.displayName);
console.log(place.photos[0]);
// Add the first photo to an img element.
const photoImg = document.getElementById('image-container');
photoImg.src = place.photos[0].getURI({maxHeight: 400});
}
Place
类中的作者署名
Place
类会以 AuthorAttribution
实例的形式返回作者署名信息,包括作者的姓名、作者 Google 个人资料页面的 URI 和作者个人资料照片的 URI。以下代码段展示了如何检索第一个照片结果的提供方信息数据。
let name = place.photos[0].authorAttributions[0].displayName;
let attributionUrl = place.photos[0].authorAttributions[0].uri;
let photoUrl = place.photos[0].authorAttributions[0].photoUri;
了解详情
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[],[],null,["# Migrate to the new Place Photos\n\n\u003cbr /\u003e\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nPlace photos lets you add high quality photographic content to your web pages.\nThis page explains the differences between place photos features in the `Place`\nclass (new) and `PlacesService` (legacy), and provides some code snippets for\ncomparison.\n\n- `PlacesService` (legacy) returns an array of up to 10 [`PlacePhoto`](/maps/documentation/javascript/reference/places-service#PlacePhoto) objects as part of the `PlaceResult` object for any `getDetails()` request if the `photos` field is specified in the request. In the case of `textSearch()` and `nearbySearch()` the first place photo is returned by default if available.\n- The `Place` class returns an array of up to 10 [`Photo`](/maps/documentation/javascript/reference/place#Photo) objects as part of a `fetchFields()` request if the `photos` field is specified in the request.\n\nThe following table lists some of the main differences in the use of place\nphotos between the `Place` class and `PlacesService`:\n\n| [`PlacesService`](/maps/documentation/javascript/reference/places-service) (Legacy) | [`Place`](/maps/documentation/javascript/reference/place) (New) |\n|-----------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| [`PlacePhoto`](/maps/documentation/javascript/reference/places-service#PlacePhoto) interface | [`Photo`](/maps/documentation/javascript/reference/place#Photo) class |\n| `PlacePhoto` returns [`html_attributions`](/maps/documentation/javascript/reference/places-service#PlacePhoto.html_attributions) as a string. | `Photo` returns an [`AuthorAttribution`](/maps/documentation/javascript/reference/place#AuthorAttribution) instance. |\n| Methods require the use of a callback to handle the results object and `google.maps.places.PlacesServiceStatus` response. | Uses Promises, and works asynchronously. |\n| Methods require a `PlacesServiceStatus` check. | No required status check, can use standard error handling. [Learn more](/maps/documentation/javascript/reference/errors). |\n| `PlacesService` must be instantiated using a map or a div element. | `Place` can be instantiated wherever needed, without a reference to a map or page element. |\n\nCode comparison\n---------------\n\nThis section compares code for place photos to illustrate the differences\nbetween the Places Service and the\nPlace class. The code snippets show the code\nrequired to request place photos on each respective API.\n\n### Places service (legacy)\n\nThe following snippet shows returning photos using `PlacesService`, and\ndisplaying the first photo result on the page. In this example, the place\ndetails request specifies a place ID, along with the `name` and `photos` fields.\nThe first photo is then displayed on the page after checking service status.\nWhen instantiating the `PlacesService`, a map or a `div` element must be\nspecified; since this example does not feature a map, a `div` element is used. \n\n function getPhotos() {\n // Construct the Place Details request.\n const request = {\n placeId: \"ChIJydSuSkkUkFQRsqhB-cEtYnw\",\n fields: [\"name\", \"photos\"],\n };\n\n // Create an instance of PlacesService.\n const attributionDiv = document.getElementById(\"attribution-div\");\n const service = new google.maps.places.PlacesService(attributionDiv);\n\n // Check status and display the first photo in an img element.\n service.getDetails(request, (place, status) =\u003e {\n if (\n status === google.maps.places.PlacesServiceStatus.OK && place\n ) {\n const photoImg = document.getElementById('image-container');\n photoImg.src = place.photos[0].getUrl({maxHeight: 400});\n }\n });\n }\n\n#### Author attributions in `PlacesService`\n\nThe `PlacesService` returns the required author attributions as an\n[`html_attributions`](/maps/documentation/javascript/reference/places-service#PlacePhoto.html_attributions)\nstring containing a URL pointing to the author's Google profile page. The\nfollowing snippet shows retrieving attribution data for the first photo result. \n\n let attributionUrl = place.photos[0].html_attributions;\n\n#### Learn more\n\n- [See the documentation](/maps/documentation/javascript/places#places_photos)\n- [`getDetails` reference](/maps/documentation/javascript/reference/places-service#PlacesService.getDetails)\n- [`PlacePhoto` interface reference](/maps/documentation/javascript/reference/places-service#PlacePhoto)\n\n### Place class (new)\n\nThe following snippet shows using the\n[`fetchFields()`](/maps/documentation/javascript/reference/place#Place.fetchFields)\nmethod to return place details including the display name and place photos.\nFirst a new `Place` object is instantiated using a place ID, followed by a call\nto `fetchFields()` where the `displayName` and `photos` fields are specified.\nThe first place photo is then displayed on the page. There is no need to check\nservice status when using the `Place` class, as this is handled automatically. \n\n async function getPhotos() {\n // Use a place ID to create a new Place instance.\n const place = new google.maps.places.Place({\n id: 'ChIJydSuSkkUkFQRsqhB-cEtYnw', // Woodland Park Zoo, Seattle WA\n });\n\n // Call fetchFields, passing the needed data fields.\n await place.fetchFields({ fields: ['displayName','photos'] });\n\n console.log(place.displayName);\n console.log(place.photos[0]);\n // Add the first photo to an img element.\n const photoImg = document.getElementById('image-container');\n photoImg.src = place.photos[0].getURI({maxHeight: 400});\n }\n\n#### Author attributions in the `Place` class\n\nThe `Place` class returns author attributions as an\n[`AuthorAttribution`](/maps/documentation/javascript/reference/place#AuthorAttribution)\ninstance including the author's name, a URI for the author's Google profile\npage, and a URI for the author's profile photo. The following snippet shows\nretrieving attribution data for the first photo result. \n\n let name = place.photos[0].authorAttributions[0].displayName;\n let attributionUrl = place.photos[0].authorAttributions[0].uri;\n let photoUrl = place.photos[0].authorAttributions[0].photoUri;\n\n#### Learn more\n\n- [See the full example](/maps/documentation/javascript/examples/place-photos)\n- [See the documentation](/maps/documentation/javascript/place-photos)\n- [`fetchFields()` reference](/maps/documentation/javascript/reference/place#Place.fetchFields)\n- [`Photo` class reference](/maps/documentation/javascript/reference/place#Photo)"]]