遷移至新的「放置相片」
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
地點相片可讓您在網頁中加入高品質的相片內容。本頁面說明 Place
類別 (新版) 和 PlacesService
(舊版) 中地點相片功能的差異,並提供一些程式碼片段以供比較。
PlacesService
(舊版) 會在任何 getDetails()
要求中,將最多 10 個 PlacePhoto
物件的陣列做為 PlaceResult
物件的一部分傳回,前提是要求中已指定 photos
欄位。如果是 textSearch()
和 nearbySearch()
,系統預設會傳回第一個地點相片 (如有)。
- 如果要求中指定了
photos
欄位,Place
類別會在 fetchFields()
要求中傳回最多 10 個 Photo
物件的陣列。
下表列出 Place
類別和 PlacesService
在使用地點相片時的一些主要差異:
程式碼比較
本節會比較地點相片的程式碼,說明 Places Service 和 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;
瞭解詳情
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間: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)"]]