अपने मैप में अलग-अलग आकार जोड़े जा सकते हैं. आकार, मैप पर मौजूद एक ऑब्जेक्ट होता है.
यह अक्षांश/देशांतर निर्देशांक से जुड़ा होता है. ये आकार उपलब्ध हैं:
लाइन, पॉलीगॉन,
सर्कल, और रेक्टैंगल. आकृतियों को इस तरह भी कॉन्फ़िगर किया जा सकता है कि उपयोगकर्ता उनमें बदलाव कर सकें या उन्हें खींचकर छोड़ सकें. इसके अलावा, जियोफ़ेंस और सुरक्षित ज़ोन की सीमाओं की जांच करने के लिए, आकृतियों को ज्यामिति लाइब्रेरी (google.maps.GeometryLibrary) के साथ जोड़ा जा सकता है.
पॉलीलाइन
अपने मैप पर लाइन बनाने के लिए, पॉलीलाइन का इस्तेमाल करें.
Polyline क्लास, मैप पर कनेक्टेड लाइन सेगमेंट के लीनियर ओवरले को तय करती है. Polyline ऑब्जेक्ट में LatLng जगहों का कलेक्शन होता है. साथ ही, यह लाइन सेगमेंट की एक सीरीज़ बनाता है, जो उन जगहों को क्रम से जोड़ती है.
पॉलीलाइन जोड़ना
Polyline कंस्ट्रक्टर, PolylineOptions का एक सेट लेता है. इससे लाइन के LatLng निर्देशांक और स्टाइल का एक सेट तय होता है. इससे पॉलीलाइन के विज़ुअल को अडजस्ट किया जा सकता है.
Polyline ऑब्जेक्ट, मैप पर सीधी लाइनों की सीरीज़ के तौर पर बनाए जाते हैं. लाइन बनाते समय, PolylineOptions में लाइन के स्ट्रोक के लिए, पसंद के हिसाब से रंग, मोटाई, और ओपैसिटी तय की जा सकती है. इसके अलावा, लाइन बनाने के बाद भी इन प्रॉपर्टी में बदलाव किया जा सकता है.
पॉलीलाइन में, स्ट्रोक की ये स्टाइल इस्तेमाल की जा सकती हैं:
strokeColor,"#FFFFFF"फ़ॉर्मैट में हेक्साडेसिमल एचटीएमएल कलर कोड तय करता है.Polylineक्लास में, रंगों के नाम इस्तेमाल नहीं किए जा सकते.strokeOpacity,0.0और1.0के बीच की कोई संख्या वाली वैल्यू तय करता है, ताकि लाइन के रंग की ओपैसिटी तय की जा सके. डिफ़ॉल्ट रूप से, यह1.0पर सेट होता है.strokeWeightसे लाइन की चौड़ाई पिक्सल में तय होती है.
पॉलीलाइन की editable प्रॉपर्टी से यह तय होता है कि उपयोगकर्ता, आकार में बदलाव कर सकते हैं या नहीं. नीचे उपयोगकर्ता के बदलाव करने लायक आकार देखें. इसी तरह, draggable प्रॉपर्टी को सेट करके, उपयोगकर्ताओं को लाइन खींचने और छोड़ने की अनुमति दी जा सकती है.
इस उदाहरण में, दो पिक्सल चौड़ी लाल पॉलीलाइन बनाई गई है. यह कैलिफ़ोर्निया के ओकलैंड और ऑस्ट्रेलिया के ब्रिस्बेन के बीच, प्रशांत महासागर के ऊपर से जाने वाली पहली फ़्लाइट का रास्ता दिखाती है.
TypeScript
// This example creates a 2-pixel-wide red polyline showing the path of // the first trans-Pacific flight between Oakland, CA, and Brisbane, // Australia which was made by Charles Kingsford Smith. const mapElement = document.querySelector('gmp-map')!; let innerMap: google.maps.Map; async function init() { const { Polyline } = await google.maps.importLibrary('maps'); innerMap = mapElement.innerMap; const flightPlanCoordinates = [ { lat: 37.772, lng: -122.214 }, { lat: 21.291, lng: -157.821 }, { lat: -18.142, lng: 178.431 }, { lat: -27.467, lng: 153.027 }, ]; const flightPath = new Polyline({ path: flightPlanCoordinates, geodesic: true, strokeColor: '#FF0000', strokeOpacity: 1.0, strokeWeight: 2, }); flightPath.setMap(innerMap); } void init();
JavaScript
// This example creates a 2-pixel-wide red polyline showing the path of // the first trans-Pacific flight between Oakland, CA, and Brisbane, // Australia which was made by Charles Kingsford Smith. const mapElement = document.querySelector('gmp-map'); let innerMap; async function init() { const { Polyline } = await google.maps.importLibrary('maps'); innerMap = mapElement.innerMap; const flightPlanCoordinates = [ { lat: 37.772, lng: -122.214 }, { lat: 21.291, lng: -157.821 }, { lat: -18.142, lng: 178.431 }, { lat: -27.467, lng: 153.027 }, ]; const flightPath = new Polyline({ path: flightPlanCoordinates, geodesic: true, strokeColor: '#FF0000', strokeOpacity: 1.0, strokeWeight: 2, }); flightPath.setMap(innerMap); } void init();
पॉलीलाइन हटाना
मैप से पॉलीलाइन हटाने के लिए, setMap() तरीके को कॉल करें. इसके लिए, null को आर्ग्युमेंट के तौर पर पास करें. यहां दिए गए उदाहरण में,
flightPath एक पॉलीलाइन ऑब्जेक्ट है:
flightPath.setMap(null);
ध्यान दें कि ऊपर बताए गए तरीके से पॉलीलाइन नहीं मिटती. यह मैप से पॉलीलाइन को हटा देता है. अगर आपको पॉलीलाइन मिटानी है, तो उसे मैप से हटाएं. इसके बाद, पॉलीलाइन को null पर सेट करें.
पॉलीलाइन की जांच करना
पॉलीलाइन, निर्देशांकों की एक सीरीज़ को LatLng ऑब्जेक्ट के ऐरे के तौर पर दिखाती है. इन निर्देशांकों से लाइन का पाथ तय होता है.
कोऑर्डिनेट पाने के लिए, getPath() को कॉल करें. इससे MVCArray टाइप का कलेक्शन दिखेगा. इन कार्रवाइयों का इस्तेमाल करके, ऐरे में बदलाव किया जा सकता है और उसकी जांच की जा सकती है:
getAt(), शून्य पर आधारित इंडेक्स वैल्यू के हिसाब सेLatLngदिखाता है.insertAt(), शून्य से शुरू होने वाले इंडेक्स की वैल्यू पर, पास किए गएLatLngको शामिल करता है. ध्यान दें कि उस इंडेक्स वैल्यू पर मौजूद कोई भी निर्देशांक, आगे की ओर बढ़ जाता है.removeAt(), शून्य पर आधारित इंडेक्स वैल्यू पर मौजूदLatLngको हटाता है.
यहां दिए गए उदाहरण में, क्लिक के आधार पर पॉलीलाइन बनाने का तरीका बताया गया है. वर्टेक्स जोड़ने के लिए, मैप पर क्लिक करें.
TypeScript
/** * This example creates an interactive map which constructs a polyline based on * user clicks. Note that the polyline only appears once its path property * contains two LatLng coordinates. */ let poly: google.maps.Polyline; const mapElement = document.querySelector('gmp-map')!; let innerMap: google.maps.Map; async function init() { // Import the needed libraries. const [{ Polyline }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), ]); innerMap = mapElement.innerMap; poly = new Polyline({ strokeColor: '#000000', strokeOpacity: 1.0, strokeWeight: 3, }); poly.setMap(innerMap); // Handles click events on a map, and adds a new point to the Polyline. innerMap.addListener('click', (event: google.maps.MapMouseEvent) => { const latLng = event.latLng; if (!latLng) return; const path = poly.getPath(); // Because path is an MVCArray, we can simply append a new coordinate // and it will automatically appear. path.push(latLng); // Add a new marker at the new plotted point on the polyline. new AdvancedMarkerElement({ position: latLng, title: '#' + path.getLength(), map: innerMap, }); }); } void init();
JavaScript
/** * This example creates an interactive map which constructs a polyline based on * user clicks. Note that the polyline only appears once its path property * contains two LatLng coordinates. */ let poly; const mapElement = document.querySelector('gmp-map'); let innerMap; async function init() { // Import the needed libraries. const [{ Polyline }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), ]); innerMap = mapElement.innerMap; poly = new Polyline({ strokeColor: '#000000', strokeOpacity: 1.0, strokeWeight: 3, }); poly.setMap(innerMap); // Handles click events on a map, and adds a new point to the Polyline. innerMap.addListener('click', (event) => { const latLng = event.latLng; if (!latLng) return; const path = poly.getPath(); // Because path is an MVCArray, we can simply append a new coordinate // and it will automatically appear. path.push(latLng); // Add a new marker at the new plotted point on the polyline. new AdvancedMarkerElement({ position: latLng, title: '#' + path.getLength(), map: innerMap, }); }); } void init();
पॉलीलाइन को पसंद के मुताबिक बनाना
पॉलीलाइन में वेक्टर-आधारित इमेज को सिंबल के तौर पर जोड़ा जा सकता है. सिंबल और PolylineOptions क्लास के कॉम्बिनेशन की मदद से, आपके पास अपने मैप पर पॉलीलाइन के लुक और स्टाइल को कंट्रोल करने के कई विकल्प होते हैं.
तीर,
डैश वाली लाइन,
कस्टम सिंबल,
और ऐनिमेशन वाले सिंबल के बारे में जानकारी पाने के लिए, सिंबल देखें.
पॉलीगॉन
पॉलीगॉन, किसी बंद पाथ (या लूप) से घिरे इलाके को दिखाता है. इसे निर्देशांकों की एक सीरीज़ से तय किया जाता है.
Polygon ऑब्जेक्ट, Polyline ऑब्जेक्ट से मिलते-जुलते होते हैं.
इनमें क्रम से लगाए गए निर्देशांकों की एक सीरीज़ होती है.
पॉलीगॉन को स्ट्रोक और फ़िल के साथ बनाया जाता है. पॉलीगॉन के किनारे (स्ट्रोक) के लिए, पसंद के हिसाब से रंग, मोटाई, और अपारदर्शिता तय की जा सकती है. साथ ही, बंद किए गए हिस्से (फ़िल) के लिए, पसंद के हिसाब से रंग और अपारदर्शिता तय की जा सकती है. रंगों को हेक्साडेसिमल एचटीएमएल फ़ॉर्मैट में दिखाया जाना चाहिए. रंगों के नाम इस्तेमाल नहीं किए जा सकते.
Polygon ऑब्जेक्ट, मुश्किल आकृतियों के बारे में बता सकते हैं. इनमें ये शामिल हैं:
- एक ही पॉलीगॉन से तय किए गए कई ऐसे इलाके जो एक-दूसरे से जुड़े नहीं हैं.
- ऐसे इलाके जहां गड्ढे हों.
- एक या इससे ज़्यादा इलाकों के इंटरसेक्शन.
किसी जटिल आकार को तय करने के लिए, कई पाथ वाले पॉलीगॉन का इस्तेमाल किया जाता है.
ध्यान दें: डेटा लेयर की मदद से, पॉलीगॉन को आसानी से ड्रॉ किया जा सकता है. यह आपके लिए पॉलीगॉन वाइंडिंग को मैनेज करता है. इससे छेद वाले पॉलीगॉन बनाना आसान हो जाता है. डेटा लेयर का दस्तावेज़ देखें.
कोई पॉलीगॉन जोड़ें
पॉलीगॉन वाले इलाके में कई अलग-अलग पाथ शामिल हो सकते हैं. इसलिए, Polygon ऑब्जेक्ट की paths प्रॉपर्टी, अरे के अरे के बारे में बताती है. हर अरे, MVCArray टाइप का होता है. हर कलेक्शन, क्रम से लगाए गए LatLng निर्देशांकों के अलग-अलग क्रम को तय करता है.
सिर्फ़ एक पाथ वाले सामान्य पॉलीगॉन के लिए, LatLng निर्देशांकों के सिंगल कलेक्शन का इस्तेमाल करके, Polygon बनाया जा सकता है. Maps JavaScript API, paths प्रॉपर्टी में स्टोर करते समय, सिंपल कलेक्शन को कलेक्शन के कलेक्शन में बदल देगा. एपीआई, एक पाथ वाले पॉलीगॉन के लिए एक आसान getPath() तरीका उपलब्ध कराता है.
पॉलीगॉन की editable प्रॉपर्टी से यह तय होता है कि उपयोगकर्ता, आकार में बदलाव कर सकते हैं या नहीं. उपयोगकर्ता के लिए, बदलाव किए जा सकने वाले आकार नीचे देखें.
इसी तरह, draggable प्रॉपर्टी को सेट करके, उपयोगकर्ताओं को आकार को खींचकर छोड़ने की अनुमति दी जा सकती है.
TypeScript
// This example creates a simple polygon representing the Bermuda Triangle. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 5, center: { lat: 24.886, lng: -70.268 }, mapTypeId: "terrain", } ); // Define the LatLng coordinates for the polygon's path. const triangleCoords = [ { lat: 25.774, lng: -80.19 }, { lat: 18.466, lng: -66.118 }, { lat: 32.321, lng: -64.757 }, { lat: 25.774, lng: -80.19 }, ]; // Construct the polygon. const bermudaTriangle = new google.maps.Polygon({ paths: triangleCoords, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35, }); bermudaTriangle.setMap(map); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example creates a simple polygon representing the Bermuda Triangle. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 5, center: { lat: 24.886, lng: -70.268 }, mapTypeId: "terrain", }); // Define the LatLng coordinates for the polygon's path. const triangleCoords = [ { lat: 25.774, lng: -80.19 }, { lat: 18.466, lng: -66.118 }, { lat: 32.321, lng: -64.757 }, { lat: 25.774, lng: -80.19 }, ]; // Construct the polygon. const bermudaTriangle = new google.maps.Polygon({ paths: triangleCoords, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35, }); bermudaTriangle.setMap(map); } window.initMap = initMap;
पॉलीगॉन के अपने-आप पूरा होने की सुविधा
ऊपर दिए गए उदाहरण में, Polygon में LatLng निर्देशांक के चार सेट शामिल हैं. हालांकि, ध्यान दें कि पहला और आखिरी सेट एक ही जगह के बारे में बताते हैं, जिससे लूप पूरा होता है. हालांकि, असल में पॉलीगॉन बंद इलाकों को तय करते हैं. इसलिए, आपको निर्देशांकों के आखिरी सेट की जानकारी देने की ज़रूरत नहीं है. Maps JavaScript API, किसी भी पाथ के लिए आखिरी जगह को पहली जगह से जोड़ने वाला स्ट्रोक बनाकर, पॉलीगॉन को अपने-आप पूरा कर देगा.
यहां दिया गया उदाहरण पिछले उदाहरण जैसा ही है. हालांकि, इसमें आखिरी LatLng को शामिल नहीं किया गया है:
उदाहरण देखें.
पॉलीगॉन हटाना
मैप से पॉलीगॉन हटाने के लिए, setMap() तरीके को कॉल करें. इसके लिए, null को आर्ग्युमेंट के तौर पर पास करें. यहां दिए गए उदाहरण में, bermudaTriangle एक पॉलीगॉन ऑब्जेक्ट है:
bermudaTriangle.setMap(null);
ध्यान दें कि ऊपर बताए गए तरीके से पॉलीगॉन नहीं मिटता. इससे मैप से पॉलीगॉन हट जाता है. अगर आपको पॉलीगॉन मिटाना है, तो उसे मैप से हटाएं. इसके बाद, पॉलीगॉन को null पर सेट करें.
किसी पॉलीगॉन की जांच करना
पॉलीगॉन, निर्देशांकों की अपनी सीरीज़ को ऐरे के ऐरे के तौर पर दिखाता है. इसमें हर ऐरे, MVCArray टाइप का होता है. हर
"लीफ" कलेक्शन, LatLng निर्देशांकों का एक कलेक्शन होता है,
जो एक पाथ के बारे में बताता है. इन निर्देशांकों को वापस पाने के लिए, Polygon ऑब्जेक्ट के getPaths() तरीके को कॉल करें. अरे एक MVCArray है, इसलिए आपको इन कार्रवाइयों का इस्तेमाल करके, इसमें बदलाव करना होगा और इसकी जांच करनी होगी:
getAt(), शून्य पर आधारित इंडेक्स वैल्यू के हिसाब सेLatLngदिखाता है.insertAt(), शून्य से शुरू होने वाले इंडेक्स की वैल्यू पर, पास किए गएLatLngको शामिल करता है. ध्यान दें कि उस इंडेक्स वैल्यू पर मौजूद कोई भी निर्देशांक, आगे की ओर बढ़ जाता है.removeAt(), शून्य पर आधारित इंडेक्स वैल्यू पर मौजूदLatLngको हटाता है.
TypeScript
// This example creates a simple polygon representing the Bermuda Triangle. // When the user clicks on the polygon an info window opens, showing // information about the polygon's coordinates. let map: google.maps.Map; let infoWindow: google.maps.InfoWindow; function initMap(): void { map = new google.maps.Map(document.getElementById("map") as HTMLElement, { zoom: 5, center: { lat: 24.886, lng: -70.268 }, mapTypeId: "terrain", }); // Define the LatLng coordinates for the polygon. const triangleCoords: google.maps.LatLngLiteral[] = [ { lat: 25.774, lng: -80.19 }, { lat: 18.466, lng: -66.118 }, { lat: 32.321, lng: -64.757 }, ]; // Construct the polygon. const bermudaTriangle = new google.maps.Polygon({ paths: triangleCoords, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 3, fillColor: "#FF0000", fillOpacity: 0.35, }); bermudaTriangle.setMap(map); // Add a listener for the click event. bermudaTriangle.addListener("click", showArrays); infoWindow = new google.maps.InfoWindow(); } function showArrays(event: any) { // Since this polygon has only one path, we can call getPath() to return the // MVCArray of LatLngs. // @ts-ignore const polygon = this as google.maps.Polygon; const vertices = polygon.getPath(); let contentString = "<b>Bermuda Triangle polygon</b><br>" + "Clicked location: <br>" + event.latLng.lat() + "," + event.latLng.lng() + "<br>"; // Iterate over the vertices. for (let i = 0; i < vertices.getLength(); i++) { const xy = vertices.getAt(i); contentString += "<br>" + "Coordinate " + i + ":<br>" + xy.lat() + "," + xy.lng(); } // Replace the info window's content and position. infoWindow.setContent(contentString); infoWindow.setPosition(event.latLng); infoWindow.open(map); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example creates a simple polygon representing the Bermuda Triangle. // When the user clicks on the polygon an info window opens, showing // information about the polygon's coordinates. let map; let infoWindow; function initMap() { map = new google.maps.Map(document.getElementById("map"), { zoom: 5, center: { lat: 24.886, lng: -70.268 }, mapTypeId: "terrain", }); // Define the LatLng coordinates for the polygon. const triangleCoords = [ { lat: 25.774, lng: -80.19 }, { lat: 18.466, lng: -66.118 }, { lat: 32.321, lng: -64.757 }, ]; // Construct the polygon. const bermudaTriangle = new google.maps.Polygon({ paths: triangleCoords, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 3, fillColor: "#FF0000", fillOpacity: 0.35, }); bermudaTriangle.setMap(map); // Add a listener for the click event. bermudaTriangle.addListener("click", showArrays); infoWindow = new google.maps.InfoWindow(); } function showArrays(event) { // Since this polygon has only one path, we can call getPath() to return the // MVCArray of LatLngs. // @ts-ignore const polygon = this; const vertices = polygon.getPath(); let contentString = "<b>Bermuda Triangle polygon</b><br>" + "Clicked location: <br>" + event.latLng.lat() + "," + event.latLng.lng() + "<br>"; // Iterate over the vertices. for (let i = 0; i < vertices.getLength(); i++) { const xy = vertices.getAt(i); contentString += "<br>" + "Coordinate " + i + ":<br>" + xy.lat() + "," + xy.lng(); } // Replace the info window's content and position. infoWindow.setContent(contentString); infoWindow.setPosition(event.latLng); infoWindow.open(map); } window.initMap = initMap;
पॉलीगॉन में छेद बनाना
किसी पॉलीगॉन के अंदर खाली जगह बनाने के लिए, आपको दो पाथ बनाने होंगे. इनमें से एक पाथ, दूसरे पाथ के अंदर होना चाहिए. छेद बनाने के लिए, अंदरूनी पाथ तय करने वाले निर्देशांक, बाहरी पाथ तय करने वाले निर्देशांकों के उल्टे क्रम में होने चाहिए. उदाहरण के लिए, अगर बाहरी पाथ के निर्देशांक घड़ी की सुई की दिशा में हैं, तो अंदरूनी पाथ के निर्देशांक घड़ी की सुई की उलटी दिशा में होने चाहिए.
ध्यान दें: डेटा लेयर, आपके लिए अंदरूनी और बाहरी पाथ के क्रम को मैनेज करती है. इससे, छेद वाले पॉलीगॉन बनाना आसान हो जाता है. डेटा लेयर के लिए दस्तावेज़ देखें.
इस उदाहरण में, दो पाथ वाला पॉलीगॉन बनाया गया है. इसमें अंदरूनी पाथ, बाहरी पाथ की तुलना में उल्टी दिशा में घुमाया गया है.
TypeScript
// This example creates a triangular polygon with a hole in it. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 5, center: { lat: 24.886, lng: -70.268 }, } ); // Define the LatLng coordinates for the polygon's outer path. const outerCoords = [ { lat: 25.774, lng: -80.19 }, { lat: 18.466, lng: -66.118 }, { lat: 32.321, lng: -64.757 }, ]; // Define the LatLng coordinates for the polygon's inner path. // Note that the points forming the inner path are wound in the // opposite direction to those in the outer path, to form the hole. const innerCoords = [ { lat: 28.745, lng: -70.579 }, { lat: 29.57, lng: -67.514 }, { lat: 27.339, lng: -66.668 }, ]; // Construct the polygon, including both paths. const bermudaTriangle = new google.maps.Polygon({ paths: [outerCoords, innerCoords], strokeColor: "#FFC107", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FFC107", fillOpacity: 0.35, }); bermudaTriangle.setMap(map); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example creates a triangular polygon with a hole in it. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 5, center: { lat: 24.886, lng: -70.268 }, }); // Define the LatLng coordinates for the polygon's outer path. const outerCoords = [ { lat: 25.774, lng: -80.19 }, { lat: 18.466, lng: -66.118 }, { lat: 32.321, lng: -64.757 }, ]; // Define the LatLng coordinates for the polygon's inner path. // Note that the points forming the inner path are wound in the // opposite direction to those in the outer path, to form the hole. const innerCoords = [ { lat: 28.745, lng: -70.579 }, { lat: 29.57, lng: -67.514 }, { lat: 27.339, lng: -66.668 }, ]; // Construct the polygon, including both paths. const bermudaTriangle = new google.maps.Polygon({ paths: [outerCoords, innerCoords], strokeColor: "#FFC107", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FFC107", fillOpacity: 0.35, }); bermudaTriangle.setMap(map); } window.initMap = initMap;
आयत
Google Maps JavaScript API में, सामान्य Polygon क्लास के अलावा,
Rectangle ऑब्जेक्ट के लिए एक खास क्लास शामिल होती है. इससे, उन्हें आसानी से बनाया जा सकता है.
रेक्टैंगल जोड़ना
Rectangle, Polygon की तरह ही होता है. इसमें रेक्टैंगल के किनारे (स्ट्रोक) के लिए, पसंद के हिसाब से रंग, मोटाई, और अपारदर्शिता तय की जा सकती है. साथ ही, रेक्टैंगल के अंदर के हिस्से (फ़िल) के लिए, पसंद के हिसाब से रंग और अपारदर्शिता तय की जा सकती है. रंगों को हेक्साडेसिमल न्यूमेरिक एचटीएमएल स्टाइल में दिखाया जाना चाहिए.
Polygon के उलट, Rectangle के लिए paths तय नहीं किया जाता. इसके बजाय, रेक्टैंगल में bounds प्रॉपर्टी होती है. यह प्रॉपर्टी, रेक्टैंगल के लिए
google.maps.LatLngBounds तय करके, उसके आकार के बारे में बताती है.
रेक्टैंगल की editable प्रॉपर्टी से यह तय होता है कि उपयोगकर्ता, शेप में बदलाव कर सकते हैं या नहीं. उपयोगकर्ता के बदलाव करने लायक
आकार नीचे देखें. इसी तरह, draggable प्रॉपर्टी को सेट करके, उपयोगकर्ताओं को रेक्टैंगल को खींचकर छोड़ने की अनुमति दी जा सकती है.
TypeScript
// This example adds a red rectangle to a map. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 11, center: { lat: 33.678, lng: -116.243 }, mapTypeId: "terrain", } ); const rectangle = new google.maps.Rectangle({ strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35, map, bounds: { north: 33.685, south: 33.671, east: -116.234, west: -116.251, }, }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example adds a red rectangle to a map. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 11, center: { lat: 33.678, lng: -116.243 }, mapTypeId: "terrain", }); const rectangle = new google.maps.Rectangle({ strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35, map, bounds: { north: 33.685, south: 33.671, east: -116.234, west: -116.251, }, }); } window.initMap = initMap;
नीचे दिया गया कोड, हर बार उपयोगकर्ता के मैप पर ज़ूम बदलने पर एक रेक्टैंगल बनाता है. रेक्टैंगल का साइज़, व्यूपोर्ट से तय होता है.
TypeScript
// This example creates a rectangle based on the viewport // on any 'zoom-changed' event. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 11, center: { lat: 40.74852, lng: -73.981687 }, mapTypeId: "terrain", } ); const rectangle = new google.maps.Rectangle(); map.addListener("zoom_changed", () => { // Get the current bounds, which reflect the bounds before the zoom. rectangle.setOptions({ strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35, map, bounds: map.getBounds() as google.maps.LatLngBounds, }); }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example creates a rectangle based on the viewport // on any 'zoom-changed' event. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 11, center: { lat: 40.74852, lng: -73.981687 }, mapTypeId: "terrain", }); const rectangle = new google.maps.Rectangle(); map.addListener("zoom_changed", () => { // Get the current bounds, which reflect the bounds before the zoom. rectangle.setOptions({ strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.35, map, bounds: map.getBounds(), }); }); } window.initMap = initMap;
रेक्टैंगल हटाना
मैप से रेक्टैंगल हटाने के लिए, setMap() तरीके को कॉल करें. इसके लिए, null को आर्ग्युमेंट के तौर पर पास करें.
rectangle.setMap(null);
ध्यान दें कि ऊपर बताए गए तरीके से रेक्टैंगल नहीं मिटता. यह मैप से रेक्टैंगल को हटा देता है. अगर आपको रेक्टैंगल को मिटाना है, तो उसे मैप से हटाएं. इसके बाद, रेक्टैंगल को null पर सेट करें.
सर्कल्स
सामान्य Polygon क्लास के अलावा, Google Maps JavaScript API में
Circle ऑब्जेक्ट के लिए एक खास क्लास शामिल है. इससे, उन्हें आसानी से बनाया जा सकता है.
कोई सर्कल जोड़ना
Circle, Polygon की तरह ही होता है. इसमें सर्कल के किनारे (स्ट्रोक) के लिए, पसंद के मुताबिक रंग, मोटाई, और ओपैसिटी तय की जा सकती है. साथ ही, सर्कल के अंदर के हिस्से (फ़िल) के लिए, पसंद के मुताबिक रंग और ओपैसिटी तय की जा सकती है. रंगों को हेक्साडेसिमल न्यूमेरिक एचटीएमएल स्टाइल में दिखाया जाना चाहिए.
Polygon के उलट, Circle के लिए paths तय नहीं किया जाता. इसके बजाय, सर्कल में दो अतिरिक्त प्रॉपर्टी होती हैं, जो इसके आकार को तय करती हैं:
centerसे वृत्त के केंद्र केgoogle.maps.LatLngका पता चलता है.radiusसे सर्कल की त्रिज्या का पता चलता है. यह मीटर में होती है.
सर्कल की editable प्रॉपर्टी से यह तय होता है कि उपयोगकर्ता, आकार में बदलाव कर सकते हैं या नहीं. उपयोगकर्ता के लिए, बदलाव किए जा सकने वाले आकार नीचे देखें.
इसी तरह, draggable प्रॉपर्टी को सेट करके, उपयोगकर्ताओं को सर्कल को खींचने और छोड़ने की अनुमति दी जा सकती है.
नीचे दिए गए उदाहरण में, जापान के क्योटो में मौजूद जगहों के बीच पैदल चलने में लगने वाले अनुमानित समय को दिखाने के लिए, सर्कल का इस्तेमाल किया गया है. मेन्यू से अपनी ज़रूरत के मुताबिक दूरी चुनें. इसके बाद, सर्कल को फिर से बीच में लाने के लिए मैप पर क्लिक करें और सर्कल को खींचकर उसकी जगह बदलें.
TypeScript
const mapElement = document.querySelector('gmp-map')!; let innerMap: google.maps.Map; async function init() { // Request needed libraries. const [{ Circle }, { AdvancedMarkerElement }, { event }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), google.maps.importLibrary('core'), ]); // Set the initial map center point. const initialCenter = { lat: 34.98956821576194, lng: 135.74239981260283 }; // Hotel Emion, Kyoto, Japan // Get the inner map. innerMap = mapElement.innerMap; // Get the buttons. const buttons = document.querySelectorAll('input[name="radius"]'); // Create the circle. const walkingCircle = new Circle({ strokeColor: '#ffdd00ff', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#ffdd00ff', fillOpacity: 0.35, map: innerMap, center: initialCenter, radius: 400, draggable: true, editable: false, }); // Define a "Crosshair" vector icon const parser = new DOMParser(); const svgString = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="-6 -6 12 12"><path d="M -6,0 L 6,0 M 0,-6 L 0,6" stroke="black" stroke-width="1"/></svg>`; const pinSvg = parser.parseFromString( svgString, 'image/svg+xml' ).documentElement; const centerMarker = new AdvancedMarkerElement({ position: initialCenter, title: 'A marker using a custom SVG image.', anchorLeft: '-50%', anchorTop: '-50%', }); centerMarker.append(pinSvg); mapElement.append(centerMarker); // Wait for the map to finish drawing its tiles. event.addListenerOnce(innerMap, 'tilesloaded', () => { // Get the controls div const controls = document.getElementById('control-panel'); // Display controls once map is loaded. if (controls) { controls.style.display = 'block'; } }); // Add event listener to update the radius based on user selection. buttons.forEach((button) => { button.addEventListener('change', (changeEvent) => { const target = changeEvent.target as HTMLInputElement; walkingCircle.setRadius(Number(target.value)); }); }); // Handle user click, reset the map center and position the circle. innerMap.addListener( 'click', ( mapsMouseEvent: google.maps.MapMouseEvent | google.maps.IconMouseEvent ) => { const newCenter = mapsMouseEvent.latLng; if (!newCenter) return; walkingCircle.setCenter(newCenter); centerMarker.position = newCenter; innerMap.panTo(newCenter); } ); // Handle user dragging the circle, update the center marker position. walkingCircle.addListener('center_changed', () => { centerMarker.position = walkingCircle.getCenter(); }); } void init();
JavaScript
const mapElement = document.querySelector('gmp-map'); let innerMap; async function init() { // Request needed libraries. const [{ Circle }, { AdvancedMarkerElement }, { event }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), google.maps.importLibrary('core'), ]); // Set the initial map center point. const initialCenter = { lat: 34.98956821576194, lng: 135.74239981260283 }; // Hotel Emion, Kyoto, Japan // Get the inner map. innerMap = mapElement.innerMap; // Get the buttons. const buttons = document.querySelectorAll('input[name="radius"]'); // Create the circle. const walkingCircle = new Circle({ strokeColor: '#ffdd00ff', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#ffdd00ff', fillOpacity: 0.35, map: innerMap, center: initialCenter, radius: 400, draggable: true, editable: false, }); // Define a "Crosshair" vector icon const parser = new DOMParser(); const svgString = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="-6 -6 12 12"><path d="M -6,0 L 6,0 M 0,-6 L 0,6" stroke="black" stroke-width="1"/></svg>`; const pinSvg = parser.parseFromString( svgString, 'image/svg+xml' ).documentElement; const centerMarker = new AdvancedMarkerElement({ position: initialCenter, title: 'A marker using a custom SVG image.', anchorLeft: '-50%', anchorTop: '-50%', }); centerMarker.append(pinSvg); mapElement.append(centerMarker); // Wait for the map to finish drawing its tiles. event.addListenerOnce(innerMap, 'tilesloaded', () => { // Get the controls div const controls = document.getElementById('control-panel'); // Display controls once map is loaded. if (controls) { controls.style.display = 'block'; } }); // Add event listener to update the radius based on user selection. buttons.forEach((button) => { button.addEventListener('change', (changeEvent) => { const target = changeEvent.target; walkingCircle.setRadius(Number(target.value)); }); }); // Handle user click, reset the map center and position the circle. innerMap.addListener('click', (mapsMouseEvent) => { const newCenter = mapsMouseEvent.latLng; if (!newCenter) return; walkingCircle.setCenter(newCenter); centerMarker.position = newCenter; innerMap.panTo(newCenter); }); // Handle user dragging the circle, update the center marker position. walkingCircle.addListener('center_changed', () => { centerMarker.position = walkingCircle.getCenter(); }); } void init();
सीएसएस
/* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } #control-panel { display: none; /* Set to 'display: block' after the map loads. */ background-color: #fff; border: 2px solid #fff; border-radius: 3px; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); font-family: 'Roboto', sans-serif; font-size: medium; margin: 10px; padding: 10px; }
एचटीएमएल
<html>
<head>
<title>Circles</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<script>
// prettier-ignore
(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: "GOOGLE_MAPS_API_KEY"
});
</script>
</head>
<body>
<gmp-map
center="34.98956821576194, 135.74239981260283"
zoom="15"
map-id="DEMO_MAP_ID">
<div id="control-panel" slot="control-inline-start-block-start">
<input
id="short-walk"
type="radio"
name="radius"
value="400"
checked />
<label for="short-walk">Short Walk (~5 minutes)</label><br />
<input
id="medium-walk"
type="radio"
name="radius"
value="800" />
<label for="medium-walk">Medium Walk (~15 minutes)</label><br />
<input id="long-walk" type="radio" name="radius" value="1600" />
<label for="long-walk">Long Walk (~30 minutes) </label>
</div>
</gmp-map>
</body>
</html>किसी सर्कल को हटाना
मैप से किसी सर्कल को हटाने के लिए, setMap() तरीके को कॉल करें. इसके लिए, null को आर्ग्युमेंट के तौर पर पास करें.
circle.setMap(null);
ध्यान दें कि ऊपर बताए गए तरीके से सर्कल नहीं मिटता. इससे मैप से सर्कल हट जाता है. अगर आपको सर्कल मिटाना है, तो उसे मैप से हटाएं. इसके बाद, सर्कल को null पर सेट करें.
खींचने और छोड़ने लायक ऐसे आकार जिनमें बदलाव किया जा सकता है
किसी आकार को बदलाव करने लायक बनाने से, आकार में हैंडल जुड़ जाते हैं. लोग इनका इस्तेमाल करके, सीधे मैप पर आकार की जगह, आकार, और साइज़ बदल सकते हैं. किसी आकार को खींचने और छोड़ने लायक भी बनाया जा सकता है, ताकि लोग उसे मैप पर किसी दूसरी जगह पर ले जा सकें.
ऑब्जेक्ट में उपयोगकर्ता ने जो बदलाव किए हैं वे सेशन के बीच सेव नहीं रहते. अगर आपको उपयोगकर्ता के बदलावों को सेव करना है, तो आपको जानकारी को खुद कैप्चर और सेव करना होगा.
किसी शेप में बदलाव करने की सुविधा चालू करना
किसी भी आकार (पॉलीलाइन, पॉलीगॉन, सर्कल, और रेक्टैंगल) को उपयोगकर्ता के लिए बदलाव करने लायक बनाया जा सकता है. इसके लिए, आकार के विकल्पों में editable को true पर सेट करें.
var bounds = { north: 44.599, south: 44.490, east: -78.443, west: -78.649 }; // Define a rectangle and set its editable property to true. var rectangle = new google.maps.Rectangle({ bounds: bounds, editable: true });
किसी शेप को खींचने और छोड़ने लायक बनाना
डिफ़ॉल्ट रूप से, मैप पर बनाया गया आकार अपनी जगह पर ही रहेगा. उपयोगकर्ताओं को मैप पर किसी आकार को खींचकर दूसरी जगह पर ले जाने की अनुमति देने के लिए, आकार के विकल्पों में draggable को true पर सेट करें.
var redCoords = [ {lat: 25.774, lng: -80.190}, {lat: 18.466, lng: -66.118}, {lat: 32.321, lng: -64.757} ]; // Construct a draggable red triangle with geodesic set to true. new google.maps.Polygon({ map: map, paths: redCoords, strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#FF0000', fillOpacity: 0.35, draggable: true, geodesic: true });
किसी पॉलीगॉन या पॉलीलाइन पर खींचने और छोड़ने की सुविधा चालू करते समय, आपको पॉलीगॉन या पॉलीलाइन को जियोडेसिक बनाने पर भी विचार करना चाहिए. इसके लिए, इसकी geodesic प्रॉपर्टी को true पर सेट करें.
जियोडेसिक पॉलीगॉन को मूव करने पर, उसका असली भौगोलिक आकार बना रहेगा. इस वजह से, मर्केटर प्रोजेक्शन में उत्तर या दक्षिण की ओर मूव करने पर, पॉलीगॉन का आकार बिगड़ा हुआ दिखेगा. नॉन-जियोडेसिक पॉलीगॉन, स्क्रीन पर हमेशा अपने शुरुआती लुक में ही दिखेंगे.
जियोडेसिक पॉलीलाइन में, पॉलीलाइन के सेगमेंट को पृथ्वी की सतह पर दो पॉइंट के बीच के सबसे छोटे रास्ते के तौर पर दिखाया जाता है. इसमें पृथ्वी को एक गोला माना जाता है, न कि मर्केटर प्रोजेक्शन पर सीधी लाइनें.
कोऑर्डिनेट सिस्टम के बारे में ज़्यादा जानकारी के लिए, मैप और टाइल कोऑर्डिनेट की गाइड देखें.
इस मैप में, करीब-करीब एक ही साइज़ और डाइमेंशन वाले दो त्रिकोण दिखाए गए हैं. लाल रंग के त्रिभुज की geodesic प्रॉपर्टी को true पर सेट किया गया है. ध्यान दें कि उत्तर की ओर बढ़ने पर, इसका आकार कैसे बदलता है.
बदलाव से जुड़े इवेंट सुनने की अनुमति
जब किसी आकार में बदलाव किया जाता है, तो बदलाव पूरा होने पर एक इवेंट ट्रिगर होता है. इन इवेंट की सूची यहां दी गई है.
| आकार | इवेंट |
|---|---|
| सर्कल |
radius_changedcenter_changed
|
| पॉलीगॉन |
insert_atremove_atset_at
लिसनर को पॉलीगॉन के पाथ पर सेट करना होगा. अगर पॉलीगॉन में एक से ज़्यादा पाथ हैं, तो हर पाथ पर एक लिसनर सेट करना होगा. |
| पॉलीलाइन |
insert_atremove_atset_at
लिसनर को पॉलीलाइन के पाथ पर सेट करना होगा. |
| रेक्टैंगल | bounds_changed |
कुछ काम के कोड स्निपेट:
google.maps.event.addListener(circle, 'radius_changed', function() { console.log(circle.getRadius()); }); google.maps.event.addListener(outerPath, 'set_at', function() { console.log('Vertex moved on outer path.'); }); google.maps.event.addListener(innerPath, 'insert_at', function() { console.log('Vertex removed from inner path.'); }); google.maps.event.addListener(rectangle, 'bounds_changed', function() { console.log('Bounds changed.'); });
रेक्टैंगल पर बदलाव करने वाले इवेंट को मैनेज करने का उदाहरण देखें: उदाहरण देखें.
ड्रैग करने से जुड़े इवेंट सुनना
जब किसी आकार को खींचा जाता है, तो खींचने की कार्रवाई की शुरुआत और अंत के साथ-साथ खींचने के दौरान भी इवेंट ट्रिगर होते हैं. पॉलीलाइन, पॉलीगॉन, सर्कल, और रेक्टैंगल के लिए, ये इवेंट ट्रिगर होते हैं.
| इवेंट | ब्यौरा |
|---|---|
dragstart |
यह इवेंट तब ट्रिगर होता है, जब उपयोगकर्ता आकार को खींचना शुरू करता है. |
drag |
जब उपयोगकर्ता किसी शेप को खींचकर छोड़ता है, तब यह इवेंट बार-बार ट्रिगर होता है. |
dragend |
यह इवेंट तब ट्रिगर होता है, जब उपयोगकर्ता आकार को खींचना बंद कर देता है. |
इवेंट मैनेज करने के बारे में ज़्यादा जानने के लिए, इवेंट से जुड़ा दस्तावेज़ देखें.
जियोफ़ेंस और सेफ़-ज़ोन की सीमाएं
Google Maps Platform, अलग से कोई जियोफ़ेंसिंग एपीआई उपलब्ध नहीं कराता. वेब मैप पर जियोफ़ेंस रेडियस या सेफ़ ज़ोन दिखाने और यह देखने के लिए कि कोई लाइव कोऑर्डिनेट उस सीमा के अंदर है या नहीं, google.maps.importLibrary("geometry") की मदद से लोड की गई ज्यामिति लाइब्रेरी (google.maps.GeometryLibrary) के साथ आकृतियों को जोड़ें:
- सर्कुलर जियोफ़ेंस:
google.maps.Circleबनाएं (याgoogle.maps.CircleLiteralपास करें) औरgoogle.maps.geometry.poly.containsLocation()का इस्तेमाल करके जांच करें कि कोऑर्डिनेट, सर्कल के अंदर है या नहीं. अगर आपकोCircle.getCenter()से मीटर में दूरी भी चाहिए, तोgoogle.maps.geometry.spherical.computeDistanceBetween()को कॉल करें. - पॉलीगॉन जियोफ़ेंस:
google.maps.Polygonबनाएं औरgoogle.maps.geometry.poly.containsLocation()का इस्तेमाल करके जांच करें कि निर्देशांक, पॉलीगॉन के अंदर है या नहीं. - 3D मैप पॉलीगॉन: 3D मैप (आकार और लाइनें) पर,
google.maps.maps3d.Polygon3DElementबनाएं. साथ ही,google.maps.geometry.poly.containsLocation()को कॉल करते समय, इसकेPolygon3DElement.pathनिर्देशांकों कोgoogle.maps.Polygonको पास करें.
यहां दिए गए उदाहरण में, एक सर्कुलर सेफ़ ज़ोन और एक पॉलीगॉनल जियोफ़ेंस बनाया गया है. इसके बाद, यह जांच की गई है कि कोई लाइव कोऑर्डिनेट हर सीमा के अंदर है या नहीं:
const {Circle, Polygon} = await google.maps.importLibrary("maps"); const {poly} = await google.maps.importLibrary("geometry"); const livePosition = {lat: 37.424, lng: -122.085}; // 1. Circular geofence (500-meter radius) const circularGeofence = new Circle({ map, center: {lat: 37.422, lng: -122.084}, radius: 500, }); const isInsideCircle = poly.containsLocation(livePosition, circularGeofence); // 2. Polygonal geofence const polygonGeofence = new Polygon({ map, paths: [ {lat: 37.420, lng: -122.088}, {lat: 37.420, lng: -122.080}, {lat: 37.426, lng: -122.080}, {lat: 37.426, lng: -122.088}, ], }); const isInsidePolygon = poly.containsLocation(livePosition, polygonGeofence);