샘플 소스 코드 전체 보기
이 예시에서는 첫 번째 장소 리뷰를 포함한 장소 세부정보를 검색하고 정보를 볼 수 있습니다.
TypeScript
let map: google.maps.Map; let centerCoordinates = { lat: 42.349134, lng: -71.083184 }; // Boston, MA let infoWindow; let contentString; async function initMap() { const { Map, InfoWindow } = await google.maps.importLibrary('maps') as google.maps.MapsLibrary; const { AdvancedMarkerElement } = await google.maps.importLibrary('marker') as google.maps.MarkerLibrary; const { Place, Review } = await google.maps.importLibrary('places') as google.maps.PlacesLibrary; map = new Map(document.getElementById('map') as HTMLElement, { center: centerCoordinates, zoom: 14, // ... }); // Use a place ID to create a new Place instance. const place = new Place({ id: 'ChIJpyiwa4Zw44kRBQSGWKv4wgA', // Faneuil Hall Marketplace, Boston, MA }); // Call fetchFields, passing 'reviews' and other needed fields. await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location', 'reviews'], }); // If there are any reviews display the first one. if (place.reviews && place.reviews.length > 0) { // Get info for the first review. let reviewRating = place.reviews[0].rating; let reviewText = place.reviews[0].text; let authorName = place.reviews[0].authorAttribution!.displayName; let authorUri = place.reviews[0].authorAttribution!.uri; // Format the review using HTML. contentString =` <div id="title"><b>${place.displayName}</b></div> <div id="address">${place.formattedAddress}</div> <a href="${authorUri}" target="_blank">Author: ${authorName}</a> <div id="rating">Rating: ${reviewRating} stars</div> <div id="rating"><p>Review: ${reviewText}</p></div>`; } else { contentString = 'No reviews were found for ' + place.displayName + '.'; } // Create an infowindow to display the review. infoWindow = new InfoWindow({ content: contentString, ariaLabel: place.displayName, }); // Add a marker. const marker = new AdvancedMarkerElement({ map, position: place.location, title: place.displayName, }); // Show the info window. infoWindow.open({ anchor: marker, map, }); } initMap();
JavaScript
let map; let centerCoordinates = { lat: 42.349134, lng: -71.083184 }; // Boston, MA let infoWindow; let contentString; async function initMap() { const { Map, InfoWindow } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); const { Place, Review } = await google.maps.importLibrary("places"); map = new Map(document.getElementById("map"), { center: centerCoordinates, zoom: 14, // ... }); // Use a place ID to create a new Place instance. const place = new Place({ id: "ChIJpyiwa4Zw44kRBQSGWKv4wgA", // Faneuil Hall Marketplace, Boston, MA }); // Call fetchFields, passing 'reviews' and other needed fields. await place.fetchFields({ fields: ["displayName", "formattedAddress", "location", "reviews"], }); // If there are any reviews display the first one. if (place.reviews && place.reviews.length > 0) { // Get info for the first review. let reviewRating = place.reviews[0].rating; let reviewText = place.reviews[0].text; let authorName = place.reviews[0].authorAttribution.displayName; let authorUri = place.reviews[0].authorAttribution.uri; // Format the review using HTML. contentString = ` <div id="title"><b>${place.displayName}</b></div> <div id="address">${place.formattedAddress}</div> <a href="${authorUri}" target="_blank">Author: ${authorName}</a> <div id="rating">Rating: ${reviewRating} stars</div> <div id="rating"><p>Review: ${reviewText}</p></div>`; } else { contentString = "No reviews were found for " + place.displayName + "."; } // Create an infowindow to display the review. infoWindow = new InfoWindow({ content: contentString, ariaLabel: place.displayName, }); // Add a marker. const marker = new AdvancedMarkerElement({ map, position: place.location, title: place.displayName, }); // Show the info window. infoWindow.open({ anchor: marker, map, }); } initMap();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html> <head> <title>Place Reviews</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div id="map"></div> <!-- 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: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "weekly"});</script> </body> </html>
샘플 사용해 보기
장소에 대한 리뷰 받기
장소에 대한 리뷰 데이터를 가져오려면 fetchFields()
에 reviews
필드를 포함합니다.
요청 매개변수를 사용할 수 있습니다. 결과 Place 인스턴스에는 Review
객체의 배열이 포함됩니다.
이곳에서 필요한 리뷰 정보에 액세스할 수 있습니다.
다음 예는 리뷰를 위해 장소 세부정보를 요청하는 방법을 보여줍니다.
// Use a place ID to create a new Place instance. const place = new Place({ id: "ChIJpyiwa4Zw44kRBQSGWKv4wgA", // Faneuil Hall Marketplace, Boston, MA }); // Call fetchFields, passing 'reviews' and other needed fields. await place.fetchFields({ fields: ["displayName", "formattedAddress", "location", "reviews"], }); // If there are any reviews display the first one. if (place.reviews && place.reviews.length > 0) { // Get info for the first review. let reviewRating = place.reviews[0].rating; let reviewText = place.reviews[0].text; let authorName = place.reviews[0].authorAttribution.displayName; let authorUri = place.reviews[0].authorAttribution.uri; // Format the review using HTML. contentString = ` <div id="title"><b>${place.displayName}</b></div> <div id="address">${place.formattedAddress}</div> <a href="${authorUri}" target="_blank">Author: ${authorName}</a> <div id="rating">Rating: ${reviewRating} stars</div> <div id="rating"><p>Review: ${reviewText}</p></div>`; } else { contentString = "No reviews were found for " + place.displayName + "."; } // Create an infowindow to display the review. infoWindow = new InfoWindow({ content: contentString, ariaLabel: place.displayName, });
Review
인스턴스에는 다음이 포함됩니다.
AuthorAttribution
- 사용자가 제공한
rating
입니다. publishTime
(날짜),relativePublishTimeDescription
(리뷰) 현재 시간을 기준으로 한 시간(예: '1개월 전')을 나타냅니다.- 리뷰
text
- 리뷰가 작성된 언어를 나타내는
textLanguageCode
입니다.
장소에 대한 전반적인 평점을 가져오려면 Place.rating
속성을 사용하세요. 이때 각 속성은
fetchFields()
요청 매개변수에서 rating
필드를 요청합니다.
저자 저작자 표시
리뷰를 표시할 때 리뷰 작성자의 저작자 표시도 표시해야 합니다. 사용
AuthorAttribution
드림
클래스를 사용하여 속성을 반환합니다. 저작자 표시에는 작성자 이름이 포함됨
(displayName
), Google 지도 프로필의 URI (uri
), URI
작성자 사진 (photoURI
) 다음 스니펫은
displayName
, uri
, photoURI
let authorName = place.reviews[0].authorAttribution!.displayName; let authorUri = place.reviews[0].authorAttribution!.uri; let authorPhoto = place.reviews[0].authorAttribution!.photoURI;