Place Photos cho phép bạn thêm nội dung ảnh chất lượng cao vào các trang web của mình. Truy cập vào hàng triệu bức ảnh được lưu trữ trong cơ sở dữ liệu Địa điểm và nhận hình ảnh có thể đổi kích thước bằng cách sử dụng các phương thức Tìm địa điểm, Tìm kiếm lân cận, Tìm kiếm bằng văn bản, Autocomplete và Chi tiết về địa điểm.
Xem mã nguồn ví dụ hoàn chỉnh
Băng chuyền ảnh thô sơ này hiển thị ảnh cho địa điểm được chỉ định, bao gồm cả thông tin ghi nhận tác giả bắt buộc (xuất hiện ở góc trên bên trái của bức ảnh được chọn).
TypeScript
async function init() { const { Place } = (await google.maps.importLibrary( 'places' )) as google.maps.PlacesLibrary; // Use a place ID to create a new Place instance. const place = new Place({ id: 'ChIJydSuSkkUkFQRsqhB-cEtYnw', // Woodland Park Zoo, Seattle WA }); // Call fetchFields, passing the desired data fields. await place.fetchFields({ fields: ['displayName', 'photos', 'editorialSummary'], }); // Get the various HTML elements. const heading = document.getElementById('heading') as HTMLElement; const summary = document.getElementById('summary') as HTMLElement; const gallery = document.getElementById('gallery') as HTMLElement; const expandedImageDiv = document.getElementById( 'expanded-image' ) as HTMLElement; // Show the display name and summary for the place. heading.textContent = place.displayName as string; summary.textContent = place.editorialSummary as string; // Add photos to the gallery. place.photos?.forEach((photo) => { const altText = 'Photo of ' + place.displayName; const img = document.createElement('img'); const imgButton = document.createElement('button'); const expandedImage = document.createElement('img'); img.src = photo?.getURI({ maxHeight: 380 }); img.alt = altText; imgButton.addEventListener('click', (event) => { centerSelectedThumbnail(imgButton); event.preventDefault(); expandedImage.src = img.src; expandedImage.alt = altText; expandedImageDiv.innerHTML = ''; expandedImageDiv.appendChild(expandedImage); const attributionLabel = createAttribution( photo.authorAttributions[0] )!; expandedImageDiv.appendChild(attributionLabel); }); imgButton.addEventListener('focus', () => { centerSelectedThumbnail(imgButton); }); imgButton.appendChild(img); gallery.appendChild(imgButton); }); // Display the first photo. if (place.photos && place.photos.length > 0) { const photo = place.photos[0]; const img = document.createElement('img'); img.alt = 'Photo of ' + place.displayName; img.src = photo.getURI(); expandedImageDiv.appendChild(img); if (photo.authorAttributions && photo.authorAttributions.length > 0) { expandedImageDiv.appendChild( createAttribution(photo.authorAttributions[0]) ); } } // Helper function to create attribution DIV. function createAttribution( attribution: google.maps.places.AuthorAttribution ) { const attributionLabel = document.createElement('a'); attributionLabel.classList.add('attribution-label'); attributionLabel.textContent = attribution.displayName; attributionLabel.href = attribution.uri!; attributionLabel.target = '_blank'; attributionLabel.rel = 'noopener noreferrer'; return attributionLabel; } // Helper function to center the selected thumbnail in the gallery. function centerSelectedThumbnail(element: HTMLElement) { element.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center', }); } } init();
JavaScript
async function init() { const { Place } = (await google.maps.importLibrary('places')); // Use a place ID to create a new Place instance. const place = new Place({ id: 'ChIJydSuSkkUkFQRsqhB-cEtYnw', // Woodland Park Zoo, Seattle WA }); // Call fetchFields, passing the desired data fields. await place.fetchFields({ fields: ['displayName', 'photos', 'editorialSummary'], }); // Get the various HTML elements. const heading = document.getElementById('heading'); const summary = document.getElementById('summary'); const gallery = document.getElementById('gallery'); const expandedImageDiv = document.getElementById('expanded-image'); // Show the display name and summary for the place. heading.textContent = place.displayName; summary.textContent = place.editorialSummary; // Add photos to the gallery. place.photos?.forEach((photo) => { const altText = 'Photo of ' + place.displayName; const img = document.createElement('img'); const imgButton = document.createElement('button'); const expandedImage = document.createElement('img'); img.src = photo?.getURI({ maxHeight: 380 }); img.alt = altText; imgButton.addEventListener('click', (event) => { centerSelectedThumbnail(imgButton); event.preventDefault(); expandedImage.src = img.src; expandedImage.alt = altText; expandedImageDiv.innerHTML = ''; expandedImageDiv.appendChild(expandedImage); const attributionLabel = createAttribution(photo.authorAttributions[0]); expandedImageDiv.appendChild(attributionLabel); }); imgButton.addEventListener('focus', () => { centerSelectedThumbnail(imgButton); }); imgButton.appendChild(img); gallery.appendChild(imgButton); }); // Display the first photo. if (place.photos && place.photos.length > 0) { const photo = place.photos[0]; const img = document.createElement('img'); img.alt = 'Photo of ' + place.displayName; img.src = photo.getURI(); expandedImageDiv.appendChild(img); if (photo.authorAttributions && photo.authorAttributions.length > 0) { expandedImageDiv.appendChild(createAttribution(photo.authorAttributions[0])); } } // Helper function to create attribution DIV. function createAttribution(attribution) { const attributionLabel = document.createElement('a'); attributionLabel.classList.add('attribution-label'); attributionLabel.textContent = attribution.displayName; attributionLabel.href = attribution.uri; attributionLabel.target = '_blank'; attributionLabel.rel = 'noopener noreferrer'; return attributionLabel; } // Helper function to center the selected thumbnail in the gallery. function centerSelectedThumbnail(element) { element.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'center', }); } } init();
CSS
/* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } #container { display: flex; border: 2px solid black; border-radius: 10px; padding: 10px; max-width: 950px; height: 100%; max-height: 400px; box-sizing: border-box; } .place-overview { width: 400px; height: 380px; overflow-x: auto; position: relative; margin-right: 20px; } #info { font-family: sans-serif; position: sticky; position: -webkit-sticky; left: 0; padding-bottom: 10px; } #heading { width: 500px; font-size: x-large; margin-bottom: 20px; } #summary { width: 100%; } #gallery { display: flex; padding-top: 10px; } #gallery img { width: 200px; height: 200px; margin: 10px; border-radius: 10px; cursor: pointer; object-fit: cover; /* fill the area without distorting the image */ } #expanded-image { display: flex; height: 370px; overflow: hidden; background-color: #000; border-radius: 10px; margin: 0 auto; } .attribution-label { background-color: rgba(255, 255, 255, 0.7); font-size: 10px; font-family: sans-serif; margin: 2px; position: absolute; } button { display: flex; outline: none; border: none; padding: 0; background: none; cursor: pointer; } button:focus { border: 2px solid blue; border-radius: 10px; }
HTML
<html lang="en">
<head>
<title>Place Photos</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<!-- prettier-ignore -->
<script>(g => { var h, a, k, p = "The Google Maps JavaScript API", c = "google", l = "importLibrary", q = "__ib__", m = document, b = window; b = b[c] || (b[c] = {}); var d = b.maps || (b.maps = {}), r = new Set, e = new URLSearchParams, u = () => h || (h = new Promise(async (f, n) => { await (a = m.createElement("script")); e.set("libraries", [...r] + ""); for (k in g) e.set(k.replace(/[A-Z]/g, t => "_" + t[0].toLowerCase()), g[k]); e.set("callback", c + ".maps." + q); a.src = `https://maps.${c}apis.com/maps/api/js?` + e; d[q] = f; a.onerror = () => h = n(Error(p + " could not load.")); a.nonce = m.querySelector("script[nonce]")?.nonce || ""; m.head.append(a) })); d[l] ? console.warn(p + " only loads once. Ignoring:", g) : d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n)) })
({ key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly" });</script>
</head>
<body>
<div id="container">
<div class="place-overview">
<div id="info">
<h1 id="heading"></h1>
<div id="summary"></div>
</div>
<div id="gallery"></div>
</div>
<div id="expanded-image"></div>
</div>
</body>
</html>Dùng thử đoạn nhạc
Lấy ảnh
Để lấy ảnh cho một địa điểm, hãy thêm trường photos vào các tham số yêu cầu fetchFields(). Phiên bản Place thu được chứa một mảng gồm tối đa 10 đối tượng Photo. Từ đó, bạn có thể truy cập vào hình ảnh và thông tin ghi nhận quyền tác giả bắt buộc của hình ảnh.
Gọi getURI() để trả về URI của ảnh nguồn, sử dụng PhotoOptions để đặt chiều cao và/hoặc chiều rộng tối đa của hình ảnh được trả về. Nếu bạn chỉ định giá trị cho cả maxHeight và maxWidth, thì dịch vụ ảnh sẽ điều chỉnh kích thước hình ảnh thành kích thước nhỏ hơn trong hai kích thước, đồng thời duy trì tỷ lệ khung hình ban đầu. Nếu bạn không chỉ định phương diện nào, thì hình ảnh có kích thước đầy đủ sẽ được trả về.
Lớp Photo hiển thị các thuộc tính sau:
authorAttributions: Một mảng gồm các đối tượngAuthorAttributionchứa văn bản và URL bắt buộc về việc ghi công.flagContentURI: Đường liên kết mà người dùng có thể dùng để báo cáo vấn đề về ảnh.googleMapsURI: Đường liên kết để hiển thị ảnh trên Google Maps.heightPx: Chiều cao của ảnh tính bằng pixel.widthPx: Chiều rộng của ảnh tính bằng pixel.
Ví dụ sau đây minh hoạ việc đưa ra yêu cầu Chi tiết về địa điểm cho ảnh, gọi getURI() trên một phiên bản ảnh để trả về URI nguồn cho hình ảnh, sau đó thêm kết quả ảnh đầu tiên vào một phần tử img (để cho ngắn gọn, chúng tôi đã bỏ qua thông tin ghi nhận quyền tác giả):
const { Place } = await google.maps.importLibrary('places'); // Use a place ID to create a new Place instance. const place = new Place({ id: 'ChIJydSuSkkUkFQRsqhB-cEtYnw', // Woodland Park Zoo, Seattle WA }); // Call fetchFields, passing the desired data fields. await place.fetchFields({ fields: ['photos'] }); // Add the first photo to an img element. const photoImg = document.getElementById('image-container'); photoImg.src = place.photos[0].getURI({maxHeight: 400});
Thông tin ghi nhận tác giả
Khi hiển thị ảnh, bạn cũng phải hiển thị thông tin ghi nhận tác giả của bức ảnh đó. Dùng lớp AuthorAttribution để trả về thông tin ghi nhận quyền tác giả. Thông tin ghi nhận quyền tác giả bao gồm tên tác giả (displayName), một URI cho hồ sơ của họ trên Google Maps (uri) và một URI cho ảnh của tác giả (photoURI). Đoạn mã sau đây cho thấy cách trả về displayName, uri và photoURI cho một bức ảnh về địa điểm.
let name = place.photos[0].authorAttributions[0].displayName; let url = place.photos[0].authorAttributions[0].uri; let authorPhoto = place.photos[0].authorAttributions[0].photoURI;