تتيح لك مراجعات الأماكن إضافة مراجعات وتقييمات المستخدمين إلى صفحات الويب. توضّح هذه الصفحة الاختلافات بين مراجعات الأماكن المستخدَمة في الفئة 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,
});