장소 리뷰를 사용하면 웹페이지에 사용자 리뷰와 평점을 추가할 수 있습니다. 이 페이지에서는 Place 클래스(신규) 및 PlacesService (기존)에서 사용되는 장소 리뷰의 차이점을 설명하고 비교를 위한 코드 스니펫을 제공합니다.
- PlacesService(레거시)은 요청에- reviews필드가 지정된 경우- getDetails()요청의- PlaceResult객체의 일부로- PlaceReview인스턴스의 배열을 반환합니다.
- Place(신규) 요청에- reviews필드가 지정된 경우- fetchFields()요청의 일부로- Review인스턴스의 배열을 반환합니다.
다음 표에는 Place 클래스와 PlacesService 간의 장소 리뷰 사용의 주요 차이점이 나와 있습니다.
| PlacesService(기존) | Place(신규) | 
|---|---|
| PlaceReview인터페이스 | Review클래스 | 
| 메서드에서는 콜백을 사용하여 결과 객체와 google.maps.places.PlacesServiceStatus응답을 처리해야 합니다. | Promise를 사용하며 비동기식으로 작동합니다. | 
| 메서드에는 PlacesServiceStatus확인이 필요합니다. | 필수 상태 확인이 없으며 표준 오류 처리를 사용할 수 있습니다. 자세히 알아보기 | 
| PlacesService는 지도 또는 div 요소를 사용하여 인스턴스화해야 합니다. | Place는 지도 또는 페이지 요소를 참조하지 않고도 필요한 곳에 인스턴스화할 수 있습니다. | 
| PlaceReview는author_name,author_url,profile_photo_url필드를 사용하여 리뷰의 기여 분석 데이터를 반환합니다. | Review은AuthorAttribution인스턴스를 사용하여 리뷰의 기여 분석 데이터를 반환합니다. | 
코드 비교
이 섹션에서는 텍스트 검색 메서드의 코드를 비교하여 기존 PlacesService의 장소 리뷰와 최신 Place 클래스 간의 차이점을 설명합니다.
장소 서비스 (기존)
다음 스니펫은 getDetails()를 호출하여 리뷰를 포함한 장소 세부정보를 요청하고 첫 번째 리뷰 결과를 정보 창에 표시합니다.
const request = {
  placeId: "ChIJpyiwa4Zw44kRBQSGWKv4wgA", // Faneuil Hall Marketplace, Boston, MA
  fields: ["name", "formatted_address", "geometry", "reviews"],
};
const service = new google.maps.places.PlacesService(map);
service.getDetails(request, (place, status) => {
  if (
    status === google.maps.places.PlacesServiceStatus.OK &&
    place &&
    place.geometry &&
    place.geometry.location
  ) {
    // 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].author_name;
      let authorUri = place.reviews[0].author_url;
      // Format the review using HTML.
      contentString =`
            <div id="title"><b>${place.name}</b></div>
            <div id="address">${place.formatted_address}</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.name}`;
    }
    const infowindow = new google.maps.InfoWindow({
      content: contentString,
      ariaLabel: place.displayName,
    });
    // Add a marker.
    const marker = new google.maps.Marker({
      map,
      position: place.geometry.location,
    });
    // Show the info window.
    infowindow.open({
      anchor: marker,
      map,
    });
  }
});
장소 클래스 (신규)
다음 스니펫은 fetchFields() 메서드를 호출하여 리뷰를 포함한 장소 세부정보를 요청하고 첫 번째 리뷰 결과를 정보 창에 표시합니다.
// Use a place ID to create a new Place instance.
const place = new google.maps.places.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 google.maps.InfoWindow({
  content: contentString,
  ariaLabel: place.displayName,
});
// Add a marker.
const marker = new google.maps.marker.AdvancedMarkerElement({
  map,
  position: place.location,
  title: place.displayName,
});
// Show the info window.
infoWindow.open({
  anchor: marker,
  map,
});