مرورهای مکان به شما امکان میدهد مرورها و ردهبندیهای کاربران را به صفحههای وب خود اضافه کنید. این صفحه
تفاوتهای بین مرورهای مکان را که در کلاس Place (جدید) و PlacesService (قدیمی) استفاده میشود توضیح میدهد و چند تکهکد برای
مقایسه ارائه میدهد.
PlacesService(قدیمی) آرایهای از نمونههایPlaceReviewرا بهعنوان بخشی از شیءPlaceResultبرای هر درخواستgetDetails()برمیگرداند، اگر فیلدreviewsدر درخواست مشخص شده باشد.-
Place(جدید) آرایهای از نمونههایReviewرا بهعنوان بخشی از درخواستfetchFields()برمیگرداند اگر فیلدreviewsدر درخواست مشخص شده باشد.
جدول زیر برخیاز تفاوتهای اصلی در استفاده از
نظرات مکان بین کلاس Place و PlacesService را فهرست میکند:
PlacesService (قدیمی) |
Place (جدید) |
|---|---|
میانا PlaceReview |
کلاس Review |
روشها برای مدیریت کردن شیء نتایج و
google.maps.places.PlacesServiceStatus پاسخ نیاز به استفاده از تماس برگشتی دارند. |
از Promises استفاده میکند و بهصورت ناهمگام کار میکند. |
روشها به بررسی 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,
});