Place Reviews की मदद से, अपने वेब पेजों पर उपयोगकर्ताओं की समीक्षाएं और रेटिंग जोड़ी जा सकती हैं. इस पेज पर, Place क्लास (नए) और PlacesService (लेगसी) में इस्तेमाल की गई जगह की समीक्षाओं के बीच के अंतर के बारे में बताया गया है. साथ ही, तुलना करने के लिए कुछ कोड स्निपेट भी दिए गए हैं.
PlacesService(लेगसी)PlaceReviewइंस्टेंस का कलेक्शन दिखाता है. यह कलेक्शन, किसी भीgetDetails()अनुरोध के लिएPlaceResultऑब्जेक्ट के हिस्से के तौर पर दिखाया जाता है. ऐसा तब होता है, जब अनुरोध में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 क्लास में, जगह की समीक्षाओं के बीच के अंतर को समझने में मदद मिलती है.
Places service (लेगसी)
यहां दिया गया स्निपेट, जगह की जानकारी का अनुरोध करने के लिए 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,
});