PlaceListElement एक एचटीएमएल एलिमेंट है, जो किसी जगह की खोज के नतीजों को सूची में रेंडर करता है. gmp-place-list
एलिमेंट को कॉन्फ़िगर करने के दो तरीके हैं:
- आस-पास के नतीजे खोजें: इस विकल्प की मदद से, विजेट को कॉन्फ़िगर करके, आस-पास की जगहें खोजें से खोज के नतीजे दिखाए जा सकते हैं.
- टेक्स्ट के अनुरोध से खोजें: इस विकल्प की मदद से, विजेट को कॉन्फ़िगर करके, जगहों की टेक्स्ट खोज से खोज के नतीजे दिखाए जा सकते हैं.
आस-पास खोजने का अनुरोध
यहां दिए गए उदाहरण में, आस-पास की जगहों की खोज के जवाब में, जगहों की सूची का एलिमेंट रेंडर किया गया है. आसानी से समझने के लिए, जगह के सिर्फ़ तीन टाइप दिए गए हैं:
कैफ़े, रेस्टोरेंट, और ईवी चार्जिंग स्टेशन. कोई नतीजा चुनने पर, चुनी गई जगह की जानकारी दिखाने वाला जानकारी वाला विंडो दिखता है. मैप में जगहों की सूची वाला एलिमेंट जोड़ने के लिए, एचटीएमएल पेज में gmp-place-list
एलिमेंट जोड़ें, जैसा कि यहां दिए गए स्निपेट में दिखाया गया है:
<gmp-map center="-37.813,144.963" zoom="10" map-id="DEMO_MAP_ID"> <div class="overlay" slot="control-inline-start-block-start"> <div class="controls"> <select name="types" class="type-select"> <option value="">Select a place type</option> <option value="cafe">Cafe</option> <option value="restaurant">Restaurant</option> <option value="electric_vehicle_charging_station"> EV charging station </option> </select> </div> <div class="list-container"> <gmp-place-list selectable></gmp-place-list> </div> </div> <gmp-place-details size="large"></gmp-place-details> </gmp-map>
इंटरैक्शन के लिए पेज एलिमेंट चुनने के लिए, कई querySelector
कॉल का इस्तेमाल किया जाता है:
const map = document.querySelector("gmp-map"); const placeList = document.querySelector("gmp-place-list"); const typeSelect = document.querySelector(".type-select"); const placeDetails = document.querySelector("gmp-place-details"); let marker = document.querySelector('gmp-advanced-marker');
जब उपयोगकर्ता खोज बटन पर क्लिक करता है, तो
configureFromSearchNearbyRequest
को कॉल किया जाता है. इसके बाद, जगह की सूची वाला एलिमेंट नतीजे दिखाता है. addMarkers
हेल्पर फ़ंक्शन में मार्कर जोड़े जाते हैं. यहां दिया गया स्निपेट, gmp-placeselect
इवेंट का इस्तेमाल करके, जगह की सूची पर क्लिक इवेंट को मैनेज करने का कोड भी दिखाता है:
typeSelect.addEventListener("change", (event) => { // First remove all existing markers. for (marker in markers) { markers[marker].map = null; } markers = {}; if (typeSelect.value) { placeList.configureFromSearchNearbyRequest({ locationRestriction: getContainingCircle(map.innerMap.getBounds()), includedPrimaryTypes: [typeSelect.value], }).then(addMarkers); // Handle user selection in Place Details. placeList.addEventListener("gmp-placeselect", ({ place }) => { markers[place.id].click(); }); } });
कोड का पूरा उदाहरण देखना
JavaScript
const map = document.querySelector("gmp-map"); const placeList = document.querySelector("gmp-place-list"); const typeSelect = document.querySelector(".type-select"); const placeDetails = document.querySelector("gmp-place-details"); let marker = document.querySelector('gmp-advanced-marker'); let markers = {}; let infoWindow; let mapCenter; async function initMap() { await google.maps.importLibrary("places"); const { InfoWindow } = await google.maps.importLibrary("maps"); const { spherical } = await google.maps.importLibrary("geometry"); infoWindow = new google.maps.InfoWindow; function getContainingCircle(bounds) { const diameter = spherical.computeDistanceBetween(bounds.getNorthEast(), bounds.getSouthWest()); const calculatedRadius = diameter / 2; const cappedRadius = Math.min(calculatedRadius, 50000); // Cap the radius to avoid an error. return { center: bounds.getCenter(), radius: cappedRadius }; } findCurrentLocation(); map.innerMap.setOptions({ mapTypeControl: false, clickableIcons: false, }); typeSelect.addEventListener("change", (event) => { // First remove all existing markers. for (marker in markers) { markers[marker].map = null; } markers = {}; if (typeSelect.value) { placeList.configureFromSearchNearbyRequest({ locationRestriction: getContainingCircle(map.innerMap.getBounds()), includedPrimaryTypes: [typeSelect.value], }).then(addMarkers); // Handle user selection in Place Details. placeList.addEventListener("gmp-placeselect", ({ place }) => { markers[place.id].click(); }); } }); } async function addMarkers() { const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); const { LatLngBounds } = await google.maps.importLibrary("core"); const bounds = new LatLngBounds(); if (placeList.places.length > 0) { placeList.places.forEach((place) => { let marker = new AdvancedMarkerElement({ map: map.innerMap, position: place.location }); markers[place.id] = marker; bounds.extend(place.location); marker.addListener('gmp-click', (event) => { if (infoWindow.isOpen) { infoWindow.close(); } placeDetails.configureFromPlace(place); placeDetails.style.width = "350px"; infoWindow.setOptions({ content: placeDetails }); infoWindow.open({ anchor: marker, map: map.innerMap }); placeDetails.addEventListener('gmp-load', () => { map.innerMap.fitBounds(place.viewport, { top: 500, left: 400 }); }); }); map.innerMap.setCenter(bounds.getCenter()); map.innerMap.fitBounds(bounds); }); } } async function findCurrentLocation() { const { LatLng } = await google.maps.importLibrary("core"); if (navigator.geolocation) { navigator.geolocation.getCurrentPosition((position) => { const pos = new LatLng(position.coords.latitude, position.coords.longitude); map.innerMap.panTo(pos); map.innerMap.setZoom(14); }, () => { console.log('The Geolocation service failed.'); map.innerMap.setZoom(14); }); } else { console.log("Your browser doesn't support geolocation"); map.innerMap.setZoom(14); } } initMap();
सीएसएस
html, body { height: 100%; margin: 0; } body { display: flex; flex-direction: column; font-family: Arial, Helvetica, sans-serif; } h1 { font-size: 16px; text-align: center; } gmp-map { box-sizing: border-box; height: 600px; } .overlay { position: relative; top: 40px; margin: 20px; width: 400px; } .controls { display: flex; gap: 10px; margin-bottom: 10px; height: 32px; } .search-button { background-color: #5491f5; color: #fff; border: 1px solid #ccc; border-radius: 5px; width: 100px; cursor: pointer; } .type-select { border: 1px solid #ccc; border-radius: 5px; flex-grow: 1; padding: 0 10px; } .list-container { height: 400px; overflow: auto; border-radius: 10px; } gmp-place-list { background-color: #fff; }
एचटीएमएल
<!DOCTYPE html> <html> <head> <title>Place List Nearby Search with Google Maps</title> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="style.css"> <script type="module" src="./index.js"></script> </head> <body> <gmp-map center="-37.813,144.963" zoom="10" map-id="DEMO_MAP_ID"> <div class="overlay" slot="control-inline-start-block-start"> <div class="controls"> <select name="types" class="type-select"> <option value="">Select a place type</option> <option value="cafe">Cafe</option> <option value="restaurant">Restaurant</option> <option value="electric_vehicle_charging_station"> EV charging station </option> </select> </div> <div class="list-container"> <gmp-place-list selectable></gmp-place-list> </div> </div> <gmp-place-details size="large"></gmp-place-details> </gmp-map> <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: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "alpha"});</script> </body> </html>
सैंपल आज़माएं
टेक्स्ट के अनुरोध से खोजना
इस उदाहरण में, हार्ड कोड किए गए टेक्स्ट की खोज के जवाब में, जगहों की सूची का एलिमेंट रेंडर किया गया है. कोई नतीजा चुनने पर, चुनी गई जगह की जानकारी दिखाने वाला एक सूचना विंडो दिखता है. मैप में जगहों की सूची वाला एलिमेंट जोड़ने के लिए, एचटीएमएल पेज में gmp-place-list
एलिमेंट जोड़ें, जैसा कि यहां दिए गए स्निपेट में दिखाया गया है:
<gmp-map center="37.395641,-122.077627" zoom="10" map-id="DEMO_MAP_ID"> <div class="overlay" slot="control-inline-start-block-start"> <div class="list-container"> <gmp-place-list selectable></gmp-place-list> </div> </div> <gmp-place-details size="large"></gmp-place-details> </gmp-map>
इंटरैक्शन के लिए पेज एलिमेंट चुनने के लिए, कई querySelector
कॉल का इस्तेमाल किया जाता है:
const map = document.querySelector("gmp-map"); const placeList = document.querySelector("gmp-place-list"); const placeDetails = document.querySelector("gmp-place-details"); let marker = document.querySelector('gmp-advanced-marker');
जब पेज लोड होने पर खोज फ़ंक्शन चलाया जाता है, तो
configureFromSearchByTextRequest
को कॉल किया जाता है और जगह की सूची वाला एलिमेंट नतीजे रेंडर करता है (addMarkers
हेल्पर फ़ंक्शन में मार्कर जोड़े जाते हैं). यहां दिया गया स्निपेट, फ़ंक्शन का कोड दिखाता है:
async function searchByTextRequest(textQuery) { if (textQuery) { placeList.configureFromSearchByTextRequest({ locationRestriction: bounds, includedType: "restaurant", textQuery: textQuery, }).then(addMarkers); // Handle user selection in Place Details. placeList.addEventListener("gmp-placeselect", ({ place }) => { markers[place.id].click(); }); } }
कोड का पूरा उदाहरण देखना
यहां दिए गए उदाहरण में,
configureFromSearchNearbyRequest
का इस्तेमाल करके, आस-पास की जगहों के हिसाब से खोज के नतीजे दिखाने के लिए, जगह की सूची वाले यूज़र इंटरफ़ेस (यूआई) कॉम्पोनेंट का इस्तेमाल किया गया है. साथ ही, मैप में क्लिक किए जा सकने वाले मार्कर जोड़े गए हैं, ताकि चुनने पर जगह की जानकारी दिख सके.
JavaScript
const map = document.querySelector("gmp-map"); const placeList = document.querySelector("gmp-place-list"); const placeDetails = document.querySelector("gmp-place-details"); let marker = document.querySelector('gmp-advanced-marker'); let markers = {}; let infoWindow; let center = { lat: 37.395641, lng: -122.077627 }; let bounds; async function initMap() { const { Map, InfoWindow } = await google.maps.importLibrary("maps"); const { Place } = await google.maps.importLibrary("places"); // Set bounds for location restriction. bounds = new google.maps.LatLngBounds({ lat: 37.37808200917261, lng: -122.13741583377849 }, { lat: 37.416676154341324, lng: -122.02261728794109 }); infoWindow = new google.maps.InfoWindow; // Center the map let adjustedCenter = offsetLatLngRight(center, -0.005); map.innerMap.panTo(adjustedCenter); map.innerMap.setZoom(14); map.innerMap.setOptions({ mapTypeControl: false, clickableIcons: false, }); searchByTextRequest('tacos in Mountain View'); } async function searchByTextRequest(textQuery) { if (textQuery) { placeList.configureFromSearchByTextRequest({ locationRestriction: bounds, includedType: "restaurant", textQuery: textQuery, }).then(addMarkers); // Handle user selection in Place Details. placeList.addEventListener("gmp-placeselect", ({ place }) => { markers[place.id].click(); }); } } async function addMarkers() { const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); const { LatLngBounds } = await google.maps.importLibrary("core"); const bounds = new LatLngBounds(); if (placeList.places.length > 0) { placeList.places.forEach((place) => { let marker = new AdvancedMarkerElement({ map: map.innerMap, position: place.location, }); markers[place.id] = marker; bounds.extend(place.location); marker.collisionBehavior = google.maps.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL; marker.addListener('gmp-click', (event) => { if (infoWindow.isOpen) { infoWindow.close(); } placeDetails.configureFromPlace(place); placeDetails.style.width = "350px"; infoWindow.setOptions({ content: placeDetails }); infoWindow.open({ anchor: marker, map: map.innerMap }); placeDetails.addEventListener('gmp-load', () => { map.innerMap.fitBounds(place.viewport, { top: 400, left: 400 }); }); }); map.innerMap.setCenter(bounds.getCenter()); map.innerMap.fitBounds(bounds); }); } } // Helper function to offset the map center. function offsetLatLngRight(latLng, longitudeOffset) { const newLng = latLng.lng + longitudeOffset; return new google.maps.LatLng(latLng.lat, newLng); } initMap();
सीएसएस
html, body { height: 100%; margin: 0; } body { display: flex; flex-direction: column; font-family: Arial, Helvetica, sans-serif; } h1 { font-size: 16px; text-align: center; } gmp-map { box-sizing: border-box; height: 500px; } .overlay { position: relative; top: 20px; margin: 20px; width: 400px; } .controls { display: flex; gap: 10px; margin-bottom: 10px; height: 32px; } .search-button { background-color: #5491f5; color: #fff; border: 1px solid #ccc; border-radius: 5px; width: 100px; cursor: pointer; } .type-select { border: 1px solid #ccc; border-radius: 5px; flex-grow: 1; padding: 0 10px; } .list-container { height: 400px; overflow: auto; border-radius: 10px; } gmp-place-list { background-color: #fff; }
एचटीएमएल
<!DOCTYPE html> <html> <head> <title>Place List Text Search with Google Maps</title> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="style.css"> <script type="module" src="./index.js"></script> </head> <body> <gmp-map center="37.395641,-122.077627" zoom="10" map-id="DEMO_MAP_ID"> <div class="overlay" slot="control-inline-start-block-start"> <div class="list-container"> <gmp-place-list selectable></gmp-place-list> </div> </div> <gmp-place-details size="large"></gmp-place-details> </gmp-map> <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: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "alpha"});</script> </body> </html>