कंट्रोल की खास जानकारी
मैप JavaScript एपीआई के ज़रिए दिखाए गए मैप में यूज़र इंटरफ़ेस (यूआई) एलिमेंट होते हैं, ताकि उपयोगकर्ता मैप के साथ इंटरैक्ट कर सके. इन एलिमेंट को कंट्रोल के तौर पर जाना जाता है. आप अपने ऐप्लिकेशन में इन कंट्रोल के अलग-अलग वर्शन शामिल कर सकते हैं. इसके अलावा, आपको कुछ करने की ज़रूरत नहीं होती और Maps JavaScript एपीआई को यह कंट्रोल दिया जा सकता है कि सब कुछ कंट्रोल किया जाए.
इस मैप में, कंट्रोल के तौर पर Maps JavaScript API के डिफ़ॉल्ट सेट दिखाए गए हैं:
नीचे उन नियंत्रणों के पूरे सेट की सूची दी गई है जिनका इस्तेमाल मैप में किया जा सकता है:
- मैप का ज़ूम लेवल बदलने के लिए, ज़ूम करने का कंट्रोल, "+" और "-" बटन को दिखाता है. यह कंट्रोल, मैप पर सबसे नीचे दाएं कोने में डिफ़ॉल्ट रूप से दिखता है.
- मैप टाइप कंट्रोल, ड्रॉपडाउन या हॉरिज़ॉन्टल बटन बार स्टाइल में उपलब्ध है, जिससे उपयोगकर्ता मैप टाइप (
ROADMAP
,SATELLITE
,HYBRID
याTERRAIN
) चुन सकता है. यह कंट्रोल, मैप के सबसे ऊपर बाएं कोने में डिफ़ॉल्ट रूप से दिखता है. - स्ट्रीट व्यू कंट्रोल में पेगमैन का आइकॉन होता है, जिसे Street View की सुविधा चालू करने के लिए मैप पर खींचा जा सकता है. यह कंट्रोल, मैप के सबसे नीचे दाईं ओर डिफ़ॉल्ट रूप से दिखता है.
- रोटेट कंट्रोल, तिरछी तस्वीरों वाले मैप के लिए, झुकाने और घूमने के विकल्पों को एक साथ दिखाता है. यह कंट्रोल, डिफ़ॉल्ट रूप से मैप के सबसे नीचे दाईं ओर दिखता है. ज़्यादा जानकारी के लिए, 45° इमेज देखें.
- स्केल कंट्रोल, मैप स्केल एलिमेंट को दिखाता है. यह कंट्रोल डिफ़ॉल्ट रूप से बंद होता है.
- फ़ुलस्क्रीन कंट्रोल, मैप को फ़ुलस्क्रीन मोड में खोलने का विकल्प देता है. यह कंट्रोल, डेस्कटॉप और मोबाइल डिवाइसों पर डिफ़ॉल्ट रूप से चालू होता है. ध्यान दें: iOS पर फ़ुलस्क्रीन स्क्रीन की सुविधा काम नहीं करती. इसलिए, iOS डिवाइसों पर फ़ुलस्क्रीन कंट्रोल नहीं दिखता.
- कीबोर्ड शॉर्टकट कंट्रोल, मैप के साथ इंटरैक्ट करने के लिए कीबोर्ड शॉर्टकट की सूची दिखाता है.
इन मैप कंट्रोल को सीधे ऐक्सेस या ऐक्सेस नहीं किया जा सकता. इसके बजाय, मैप के MapOptions
फ़ील्ड में बदलाव किया जाता है. इस बदलाव से कंट्रोल के दिखने और उसे दिखाने पर असर पड़ता है. मैप को इंस्टैंशिएट करने के बाद, कंट्रोल प्रज़ेंटेशन को अडजस्ट किया जा सकता है. साथ ही, मैप के विकल्पों को बदलने के लिए, setOptions()
को कॉल करके मैप को डाइनैमिक तरीके से बदला जा सकता है.MapOptions
ये सभी कंट्रोल, डिफ़ॉल्ट रूप से चालू नहीं होते. डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) व्यवहार के बारे में जानने और ऐसा व्यवहार बदलने का तरीका जानने के लिए, नीचे दिया गया डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) देखें.
डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई)
अगर मैप बहुत छोटा है, तो सभी कंट्रोल डिफ़ॉल्ट तौर पर गायब हो जाते हैं (200x200 पिक्सल). इस सेटिंग को बदलने के लिए, कंट्रोल को साफ़ तौर पर दिखाएं. मैप में कंट्रोल जोड़ना देखें.
कंट्रोल की सूची में बताए गए व्यवहार (फ़ुलस्क्रीन कंट्रोल) को छोड़कर, सभी मोबाइल और डेस्कटॉप डिवाइसों पर कंट्रोल के दिखने और दिखने का तरीका एक जैसा होता है.
इसके अलावा, सभी डिवाइसों पर कीबोर्ड हैंडलिंग की सुविधा डिफ़ॉल्ट रूप से चालू रहती है.
डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) बंद करना
हो सकता है कि आप एपीआई के डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) बटन को पूरी तरह बंद करना चाहें. ऐसा करने के लिए, मैप की disableDefaultUI
प्रॉपर्टी को (MapOptions
ऑब्जेक्ट में) true
पर सेट करें. यह प्रॉपर्टी, Maps JavaScript API से यूज़र इंटरफ़ेस (यूआई) कंट्रोल बटन को बंद कर देती है. हालांकि, बुनियादी मैप पर माउस के जेस्चर या कीबोर्ड शॉर्टकट से ऐसे यूआरएल पर कोई असर नहीं पड़ता जिसे gestureHandling
और keyboardShortcuts
प्रॉपर्टी से कंट्रोल किया जाता है.
नीचे दिया गया कोड, यूज़र इंटरफ़ेस (यूआई) बटन को बंद कर देता है:
TypeScript
function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 4, center: { lat: -33, lng: 151 }, disableDefaultUI: true, } ); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 4, center: { lat: -33, lng: 151 }, disableDefaultUI: true, }); } window.initMap = initMap;
सैंपल आज़माएं
मैप में कंट्रोल जोड़ना
यूज़र इंटरफ़ेस (यूआई) के व्यवहार या कंट्रोल को हटाकर, जोड़कर या उनमें बदलाव करके, अपने इंटरफ़ेस को बेहतर बनाया जा सकता है. साथ ही, यह पक्का किया जा सकता है कि आने वाले समय में अपडेट करने पर, इस व्यवहार में कोई बदलाव न हो. अगर आपको सिर्फ़ मौजूदा व्यवहार जोड़ना या उसमें बदलाव करना है, तो आपको यह पक्का करना होगा कि आपके ऐप्लिकेशन में कंट्रोल साफ़ तौर पर जोड़ा गया है.
कुछ कंट्रोल डिफ़ॉल्ट रूप से मैप पर दिखते हैं. हालांकि, अन्य कंट्रोल तब तक नहीं दिखते, जब तक आपके अनुरोध नहीं किए जाते. मैप में कंट्रोल जोड़ने या हटाने की सुविधा इन MapOptions
ऑब्जेक्ट के फ़ील्ड में दी गई है. इन्हें true
दिखाने के लिए सेट किया गया है या इन्हें छिपाने के लिए, false
पर सेट किया गया है:
{ zoomControl: boolean, mapTypeControl: boolean, scaleControl: boolean, streetViewControl: boolean, rotateControl: boolean, fullscreenControl: boolean }
अगर मैप का साइज़ 200x200 पिक्सल से कम है, तो डिफ़ॉल्ट रूप से सभी कंट्रोल हट जाते हैं.
इस सेटिंग को बदलने के लिए, कंट्रोल को साफ़ तौर पर दिखाएं. उदाहरण के लिए, मैप के साइज़ और zoomControl
फ़ील्ड की सेटिंग के आधार पर, नीचे दी गई टेबल से पता चलता है कि ज़ूम कंट्रोल दिख रहा है या नहीं:
मैप आकार | zoomControl |
क्या दिख रहा है? |
---|---|---|
कोई भी | false |
नहीं |
कोई भी | true |
हां |
>= 200x200 पिक्सल | undefined |
हां |
< 200x200 पिक्सल | undefined |
नहीं |
यहां दिया गया उदाहरण, ज़ूम कंट्रोल को छिपाने और स्केल कंट्रोल को दिखाने के लिए, मैप सेट करता है. ध्यान दें कि हम डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) को साफ़ तौर पर बंद नहीं करते. इसलिए, ये बदलाव डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) के व्यवहार में शामिल होते हैं.
TypeScript
function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 4, center: { lat: -33, lng: 151 }, zoomControl: false, scaleControl: true, } ); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 4, center: { lat: -33, lng: 151 }, zoomControl: false, scaleControl: true, }); } window.initMap = initMap;
सैंपल आज़माएं
कंट्रोल के विकल्प
कई कंट्रोल कॉन्फ़िगर किए जा सकते हैं. इनकी मदद से, उनके व्यवहार या दिखने के तरीके को बदला जा सकता है. उदाहरण के लिए, मैप टाइप कंट्रोल, हॉरिज़ॉन्टल बार या ड्रॉपडाउन मेन्यू के तौर पर दिख सकता है.
मैप बनाने के बाद, MapOptions
ऑब्जेक्ट में कंट्रोल के विकल्प
के सही फ़ील्ड को बदलकर, इन कंट्रोल में बदलाव किया जाता है.
उदाहरण के लिए, मैप टाइप कंट्रोल में बदलाव करने के विकल्पों को mapTypeControlOptions
फ़ील्ड में दिखाया गया है. मैप टाइप कंट्रोल नीचे दिए गए style
विकल्पों में से किसी एक में दिख सकता है:
google.maps.MapTypeControlStyle.HORIZONTAL_BAR
, Google Maps पर दिखाए गए कंट्रोल की श्रेणी को एक हॉरिज़ॉन्टल बार में दिखाता है.google.maps.MapTypeControlStyle.DROPDOWN_MENU
एक बटन पर कंट्रोल दिखाता है. इसकी मदद से, ड्रॉपडाउन मेन्यू से मैप का टाइप चुना जा सकता है.google.maps.MapTypeControlStyle.DEFAULT
, डिफ़ॉल्ट तरीका दिखाता है, जो स्क्रीन के साइज़ पर निर्भर करता है. यह एपीआई के आने वाले वर्शन में बदल सकता है.
ध्यान दें कि अगर आपने कंट्रोल करने के किसी भी विकल्प में बदलाव किया है, तो आपको MapOptions
के साथ true
को सही तरीके से सेट करके, कंट्रोल को भी चालू करना होगा. उदाहरण के लिए, DROPDOWN_MENU
स्टाइल दिखाने के लिए मैप टाइप कंट्रोल सेट करने के लिए, MapOptions
ऑब्जेक्ट में नीचे दिए गए कोड का इस्तेमाल करें:
... mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU } ...
नीचे दिए गए उदाहरण में, कंट्रोल की डिफ़ॉल्ट जगह और स्टाइल को बदलने का तरीका बताया गया है.
TypeScript
// You can set control options to change the default position or style of many // of the map controls. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 4, center: { lat: -33, lng: 151 }, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, mapTypeIds: ["roadmap", "terrain"], }, } ); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// You can set control options to change the default position or style of many // of the map controls. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 4, center: { lat: -33, lng: 151 }, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, mapTypeIds: ["roadmap", "terrain"], }, }); } window.initMap = initMap;
सैंपल आज़माएं
कंट्रोल, आम तौर पर मैप बनाए जाने पर कॉन्फ़िगर होते हैं. हालांकि, Map
के setOptions()
तरीके को कॉल करके, कंट्रोल को प्रज़ेंट करने की सुविधा को डाइनैमिक तौर पर बदला जा सकता है. हालांकि, इसके लिए आपने कंट्रोल के नए विकल्प पास किए हैं.
कंट्रोल में बदलाव करना
जब आप मैप के MapOptions
ऑब्जेक्ट में फ़ील्ड के ज़रिए अपना मैप बनाते हैं, तब कंट्रोल की प्रज़ेंटेशन तय की जाती है. ये
फ़ील्ड नीचे दिए गए हैं:
zoomControl
ज़ूम कंट्रोल को चालू/बंद करता है. डिफ़ॉल्ट रूप से, यह कंट्रोल आपको मैप पर सबसे नीचे दाईं ओर दिखता है. इसके अलावा,zoomControlOptions
फ़ील्ड इस कंट्रोल के लिए,ZoomControlOptions
का इस्तेमाल भी करता है.mapTypeControl
मैप टाइप कंट्रोल को चालू/बंद करता है, जिसकी मदद से उपयोगकर्ता मैप टाइप (जैसे, मैप और सैटलाइट) के बीच टॉगल कर सकते हैं. डिफ़ॉल्ट रूप से, यह कंट्रोल आपको मैप पर सबसे ऊपर बाएं कोने में दिखता है. इसके अलावा,mapTypeControlOptions
फ़ील्ड इस कंट्रोल के लिए इस्तेमाल किए जाने वालेMapTypeControlOptions
के बारे में भी बताता है.streetViewControl
पेगमैन कंट्रोल को चालू/बंद करता है जिसकी मदद से उपयोगकर्ता 'स्ट्रीट व्यू' के पैनोरामा को चालू कर सकते हैं. डिफ़ॉल्ट रूप से यह कंट्रोल, मैप पर सबसे नीचे दाईं ओर दिखता है. इसके अलावा,streetViewControlOptions
फ़ील्ड इस कंट्रोल के लिए इस्तेमाल किए जाने वालेStreetViewControlOptions
के बारे में भी बताता है.- 45° वाली तस्वीरों के ओरिएंटेशन को कंट्रोल करने के लिए, 'रोटेट करें' कंट्रोल दिखने के तरीके को
rotateControl
चालू/बंद करता है. डिफ़ॉल्ट रूप से, कंट्रोल की मौजूदगी को इस बात से तय किया जाता है कि मौजूदा ज़ूम और जगह के मैप पर 45° वाली इमेज मौजूद है या नहीं. इस्तेमाल करने के लिएRotateControlOptions
तय करने के लिए, मैप काrotateControlOptions
सेट करके, कंट्रोल के व्यवहार को बदला जा सकता है. अगर फ़िलहाल 45° वाली कोई इमेज उपलब्ध नहीं है, तो आपके पास उसे कंट्रोल करने का विकल्प नहीं होगा. scaleControl
, स्केल कंट्रोल को चालू/बंद करता है. इससे, मैप को बड़ा स्केल मिलता है. डिफ़ॉल्ट रूप से, यह कंट्रोल नहीं दिखता. चालू होने पर, यह हमेशा मैप के सबसे नीचे दाएं कोने में दिखेगा.scaleControlOptions
, इस कंट्रोल के लिए इस्तेमाल करने के लिएScaleControlOptions
के बारे में भी बताता है.fullscreenControl
उस कंट्रोल को चालू/बंद कर देता है जो मैप को फ़ुलस्क्रीन मोड में खोलता है. डिफ़ॉल्ट रूप से, यह कंट्रोल डेस्कटॉप और Android डिवाइसों पर डिफ़ॉल्ट रूप से चालू रहता है. इस विकल्प के चालू होने पर कंट्रोल, मैप के सबसे ऊपर दाईं ओर दिखता है.fullscreenControlOptions
, इस कंट्रोल के लिए इस्तेमाल करने के लिएFullscreenControlOptions
के बारे में भी बताता है.
ध्यान दें कि उन कंट्रोल के लिए विकल्प तय किए जा सकते हैं जिन्हें आपने शुरुआत में बंद किया हो.
कंट्रोल पोज़िशनिंग
कंट्रोल के ज़्यादातर विकल्पों में एक position
प्रॉपर्टी (ControlPosition
टाइप की) होती है. इससे पता चलता है कि मैप पर कंट्रोल कहां डालना है. इन कंट्रोल की रैंकिंग पूरी तरह नहीं है. इसके बजाय, एपीआई दिए गए कंस्ट्रेंट (जैसे कि मैप का साइज़) में, मौजूदा मैप एलिमेंट या दूसरे कंट्रोल के हिसाब से कंट्रोल करके, उन्हें सही तरीके से कंट्रोल करेगा.
ध्यान दें: इस बात की कोई गारंटी नहीं दी जा सकती कि कंट्रोल, दिए गए लेआउट को ओवरलैप नहीं कर सकते. हालांकि, एपीआई उन्हें सही तरीके से व्यवस्थित करने की कोशिश करेगा.
कंट्रोल की ये स्थितियां काम करती हैं:
TOP_CENTER
यह बताता है कि कंट्रोल को मैप के सबसे ऊपर बीच में रखा जाना चाहिए.TOP_LEFT
यह बताता है कि कंट्रोल को मैप के सबसे ऊपर बाईं ओर रखना चाहिए. साथ ही, कंट्रोल के किसी भी सब-एलिमेंट को टॉप सेंटर की ओर रखना चाहिए.TOP_RIGHT
से यह पता चलता है कि कंट्रोल को मैप के सबसे ऊपर दाईं ओर रखा जाना चाहिए. साथ ही, कंट्रोल के किसी भी सब-एलिमेंट को टॉप सेंटर की ओर रखना चाहिए.LEFT_TOP
से यह पता चलता है कि कंट्रोल को मैप के सबसे ऊपर बाईं ओर रखा जाना चाहिए. हालांकि, इसेTOP_LEFT
एलिमेंट के नीचे रखा जाना चाहिए.RIGHT_TOP
से यह पता चलता है कि कंट्रोल को मैप के सबसे ऊपर दाईं ओर रखा जाना चाहिए. हालांकि, इसे किसीTOP_RIGHT
एलिमेंट के नीचे रखा जाना चाहिए.LEFT_CENTER
से यह पता चलता है कि कंट्रोल को मैप के बाईं ओर रखा जाना चाहिए. यहTOP_LEFT
औरBOTTOM_LEFT
पोज़िशन के बीच होना चाहिए.RIGHT_CENTER
बताता है कि कंट्रोल को मैप के दाईं ओर रखा जाना चाहिए. यहTOP_RIGHT
औरBOTTOM_RIGHT
पोज़िशन के बीच होना चाहिए.LEFT_BOTTOM
से यह पता चलता है कि कंट्रोल को मैप के सबसे नीचे बाईं ओर, और किसी भीBOTTOM_LEFT
एलिमेंट के ऊपर रखा जाना चाहिए.RIGHT_BOTTOM
से यह पता चलता है कि कंट्रोल को मैप के सबसे नीचे दाईं ओर, और किसी भीBOTTOM_RIGHT
एलिमेंट के ऊपर रखा जाना चाहिए.BOTTOM_CENTER
यह बताता है कि कंट्रोल को मैप के सबसे नीचे बीच में रखा जाना चाहिए.BOTTOM_LEFT
से यह पता चलता है कि कंट्रोल को मैप के सबसे नीचे बाईं ओर रखा जाना चाहिए. इसमें कंट्रोल के सभी सब-एलिमेंट, नीचे के बीच की तरफ़ होते हैं.BOTTOM_RIGHT
से यह पता चलता है कि कंट्रोल को मैप के सबसे नीचे दाईं ओर रखा जाना चाहिए. साथ ही, कंट्रोल के किसी भी सब-एलिमेंट को नीचे वाले सेंटर की ओर रखा जाना चाहिए.
ध्यान दें कि ये पोज़िशन, यूज़र इंटरफ़ेस (यूआई) एलिमेंट के ऐसे एलिमेंट से मेल खा सकती हैं जिनके प्लेसमेंट में बदलाव नहीं किया जा सकता. जैसे, कॉपीराइट और Google लोगो. उन मामलों में, कंट्रोल हर एक पोज़िशन के लिए बताए गए लॉजिक के हिसाब से फ़्लो होंगे. साथ ही, वे बताई गई जगह पर जितना हो सके उतना करीब दिखेंगे.
नीचे दिए गए उदाहरण में एक आसान मैप दिखाया गया है, जिसमें अलग-अलग जगहों पर दिए गए कंट्रोल मौजूद हैं.
TypeScript
function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 12, center: { lat: -28.643387, lng: 153.612224 }, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, position: google.maps.ControlPosition.TOP_CENTER, }, zoomControl: true, zoomControlOptions: { position: google.maps.ControlPosition.LEFT_CENTER, }, scaleControl: true, streetViewControl: true, streetViewControlOptions: { position: google.maps.ControlPosition.LEFT_TOP, }, fullscreenControl: true, } ); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: { lat: -28.643387, lng: 153.612224 }, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, position: google.maps.ControlPosition.TOP_CENTER, }, zoomControl: true, zoomControlOptions: { position: google.maps.ControlPosition.LEFT_CENTER, }, scaleControl: true, streetViewControl: true, streetViewControlOptions: { position: google.maps.ControlPosition.LEFT_TOP, }, fullscreenControl: true, }); } window.initMap = initMap;
सैंपल आज़माएं
कस्टम कंट्रोल
मौजूदा एपीआई कंट्रोल की स्टाइल और पोज़िशन में बदलाव करने के साथ-साथ,
अपने कंट्रोल भी बनाए जा सकते हैं, ताकि उपयोगकर्ता के साथ इंटरैक्शन को मैनेज किया जा सके. कंट्रोल, स्टेशनरी विजेट होते हैं, जो सटीक जानकारी के साथ मैप पर सबसे ऊपर आते हैं. ये विजेट, ओवरले के ठीक उलट होते हैं जो मूल मैप से मूव होते हैं. ज़्यादा बुनियादी बात यह है कि कंट्रोल एक ऐसा <div>
एलिमेंट है जिसका मैप पर ऐब्सलूट पोज़िशन है और यह उपयोगकर्ता को कुछ यूज़र इंटरफ़ेस (यूआई) दिखाता है. इसके अलावा, आम तौर पर इवेंट या हैंडलर के ज़रिए उपयोगकर्ता या मैप के साथ इंटरैक्शन को हैंडल करता है.
अपनी पसंद के मुताबिक कंट्रोल बनाने के लिए, कुछ नियम ज़रूरी हैं. हालांकि, ये दिशा-निर्देश सबसे सही तरीके के तौर पर काम कर सकते हैं:
- दिखाए जाने वाले कंट्रोल एलिमेंट के लिए, सही सीएसएस तय करें.
- मैप प्रॉपर्टी में बदलाव या उपयोगकर्ता इवेंट (उदाहरण के लिए,
'click'
इवेंट) के लिए, इवेंट या हैंडलर के ज़रिए, उपयोगकर्ता के साथ इंटरैक्शन मैनेज करें. - कंट्रोल एलिमेंट को होल्ड करने के लिए,
<div>
एलिमेंट बनाएं और इस एलिमेंट कोMap
कीcontrols
प्रॉपर्टी में जोड़ें.
इनमें से हर समस्या के बारे में नीचे चर्चा की गई है.
कस्टम कंट्रोल बनाना
यह आपको तय करना होता है कि कंट्रोल कैसे बनाया जाए. आम तौर पर, हमारा सुझाव है कि आप अपने सभी कंट्रोल प्रज़ेंटेशन को एक ही <div>
एलिमेंट में रखें, ताकि आप अपने कंट्रोल को एक यूनिट के तौर पर बदल सकें. हम नीचे दिखाए गए नमूनों में, इस डिज़ाइन पैटर्न का इस्तेमाल करेंगे.
ध्यान खींचने वाले कंट्रोल डिज़ाइन करने के लिए, आपको सीएसएस और डीओएम स्ट्रक्चर की जानकारी होनी चाहिए. नीचे दिया गया कोड एक बटन एलिमेंट बनाने का फ़ंक्शन दिखाता है, जो मैप को शिकागो पर केंद्रित करने के लिए पैन करता है.
function createCenterControl(map) { const controlButton = document.createElement("button"); // Set CSS for the control. controlButton.style.backgroundColor = "#fff"; controlButton.style.border = "2px solid #fff"; controlButton.style.borderRadius = "3px"; controlButton.style.boxShadow = "0 2px 6px rgba(0,0,0,.3)"; controlButton.style.color = "rgb(25,25,25)"; controlButton.style.cursor = "pointer"; controlButton.style.fontFamily = "Roboto,Arial,sans-serif"; controlButton.style.fontSize = "16px"; controlButton.style.lineHeight = "38px"; controlButton.style.margin = "8px 0 22px"; controlButton.style.padding = "0 5px"; controlButton.style.textAlign = "center"; controlButton.textContent = "Center Map"; controlButton.title = "Click to recenter the map"; controlButton.type = "button"; // Setup the click event listeners: simply set the map to Chicago. controlButton.addEventListener("click", () => { map.setCenter(chicago); }); return controlButton; }
कस्टम कंट्रोल से इवेंट मैनेज करना
किसी कंट्रोल के काम आने के लिए ज़रूरी है कि वह कुछ काम करे. यह आप पर निर्भर करता है कि आप क्या कंट्रोल करें. यह कंट्रोल, उपयोगकर्ता के इनपुट का जवाब दे सकता है या Map
के स्टेटस में होने वाले बदलावों के हिसाब से काम कर सकता है.
उपयोगकर्ता के इनपुट का जवाब देने के लिए, addEventListener()
का इस्तेमाल करें. यह काम करने वाले DOM इवेंट को मैनेज करता है. नीचे दिया गया कोड स्निपेट, ब्राउज़र के 'click'
इवेंट के लिए, लिसनर जोड़ता है. ध्यान दें कि इस इवेंट को डीओएम से मिला है, मैप से नहीं.
// Setup the click event listener: set the map to center on Chicago var chicago = {lat: 41.850, lng: -87.650}; controlButton.addEventListener('click', () => { map.setCenter(chicago); });
कस्टम कंट्रोल को ऐक्सेस करने लायक बनाना
यह पक्का करने के लिए कि कंट्रोल, कीबोर्ड इवेंट पाने और स्क्रीन रीडर को सही तरीके से दिखाने के लिए:
- बटन, फ़ॉर्म एलिमेंट, और लेबल के लिए, हमेशा नेटिव एचटीएमएल एलिमेंट का इस्तेमाल करें. कंटेनर के तौर पर DIV एलिमेंट का इस्तेमाल करें, ताकि आपको नेटिव कंट्रोल को होल्ड पर रखा जा सके. इसके अलावा, DIV एलिमेंट को इंटरैक्टिव यूज़र इंटरफ़ेस (यूआई) एलिमेंट के तौर पर फिर से इस्तेमाल न करें.
- जहां भी ज़रूरी हो, किसी यूज़र इंटरफ़ेस (यूआई) के बारे में जानकारी देने के लिए,
label
एलिमेंट,title
एट्रिब्यूट याaria-label
एट्रिब्यूट का इस्तेमाल करें.
पोज़िशनिंग कस्टम कंट्रोल
कस्टम कंट्रोल, मैप पर Map
ऑब्जेक्ट के controls
प्रॉपर्टी में सही पोज़िशन पर रखे जाते हैं. इस प्रॉपर्टी में google.maps.ControlPosition
की कैटगरी शामिल है. सही
ControlPosition
में Node
(आम तौर पर <div>
) जोड़कर,
मैप पर अपनी पसंद के मुताबिक कंट्रोल जोड़ें. (इन स्थितियों के बारे में
जानकारी के लिए, ऊपर दी गई कंट्रोल की पोज़िशनिंग
देखें.)
हर ControlPosition
उस जगह पर दिखाए गए MVCArray
कंट्रोल सेव करता है. नतीजों के दिखने के बाद, इन पर कंट्रोल जोड़े या हटाए जाते हैं. इससे एपीआई, कंट्रोल को उसी हिसाब से अपडेट कर देता है.
एपीआई हर जगह पर कंट्रोल को index
प्रॉपर्टी के क्रम में रखता है; कम इंडेक्स वाले कंट्रोल को पहले रखा जाता है.
उदाहरण के लिए, BOTTOM_RIGHT
रैंक पर दो कस्टम कंट्रोल, इंडेक्स करने के इस क्रम के हिसाब से तय किए जाएंगे. हालांकि, इंडेक्स करने की कम वैल्यू को प्राथमिकता दी जाएगी. डिफ़ॉल्ट रूप से, सभी कस्टम कंट्रोल, एपीआई के डिफ़ॉल्ट कंट्रोल डालने के बाद
रखे जाते हैं. कंट्रोल की index
प्रॉपर्टी को नेगेटिव वैल्यू पर सेट करके, इस व्यवहार को बदला जा सकता है. कस्टम कंट्रोल, लोगो के बाईं ओर या कॉपीराइट की दाईं ओर नहीं रखे जा सकते.
यह कोड, एक नया कस्टम कंट्रोल बनाता है (इसका कंस्ट्रक्टर नहीं दिखता) और इसे TOP_RIGHT
पोज़िशन पर मैप में जोड़ता है.
var map = new google.maps.Map(document.getElementById('map'), mapOptions); // Create a DIV to attach the control UI to the Map. const centerControlDiv = document.createElement("div"); // Create the control. This code calls a function that // creates a new instance of a button control. const centerControl = createCenterControl(map); // Append the control to the DIV. centerControlDiv.appendChild(centerControl); // Add the control to the map at a designated control position // by pushing it on the position's array. This code will // implicitly add the control to the DOM, through the Map // object. You should not attach the control manually. map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
कस्टम कंट्रोल का उदाहरण
नीचे दिया गया कंट्रोल आसान है (हालांकि, यह खास तौर पर उपयोगी नहीं है) और
ऊपर दिखाए गए पैटर्न को जोड़ता है. यह कंट्रोल, किसी खास जगह पर मैप लगाकर, डीओएम 'click'
इवेंट का जवाब देता है:
TypeScript
let map: google.maps.Map; const chicago = { lat: 41.85, lng: -87.65 }; /** * Creates a control that recenters the map on Chicago. */ function createCenterControl(map) { const controlButton = document.createElement('button'); // Set CSS for the control. controlButton.style.backgroundColor = '#fff'; controlButton.style.border = '2px solid #fff'; controlButton.style.borderRadius = '3px'; controlButton.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)'; controlButton.style.color = 'rgb(25,25,25)'; controlButton.style.cursor = 'pointer'; controlButton.style.fontFamily = 'Roboto,Arial,sans-serif'; controlButton.style.fontSize = '16px'; controlButton.style.lineHeight = '38px'; controlButton.style.margin = '8px 0 22px'; controlButton.style.padding = '0 5px'; controlButton.style.textAlign = 'center'; controlButton.textContent = 'Center Map'; controlButton.title = 'Click to recenter the map'; controlButton.type = 'button'; // Setup the click event listeners: simply set the map to Chicago. controlButton.addEventListener('click', () => { map.setCenter(chicago); }); return controlButton; } function initMap() { map = new google.maps.Map(document.getElementById('map') as HTMLElement, { zoom: 12, center: chicago, }); // Create the DIV to hold the control. const centerControlDiv = document.createElement('div'); // Create the control. const centerControl = createCenterControl(map); // Append the control to the DIV. centerControlDiv.appendChild(centerControl); map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
let map; const chicago = { lat: 41.85, lng: -87.65 }; /** * Creates a control that recenters the map on Chicago. */ function createCenterControl(map) { const controlButton = document.createElement("button"); // Set CSS for the control. controlButton.style.backgroundColor = "#fff"; controlButton.style.border = "2px solid #fff"; controlButton.style.borderRadius = "3px"; controlButton.style.boxShadow = "0 2px 6px rgba(0,0,0,.3)"; controlButton.style.color = "rgb(25,25,25)"; controlButton.style.cursor = "pointer"; controlButton.style.fontFamily = "Roboto,Arial,sans-serif"; controlButton.style.fontSize = "16px"; controlButton.style.lineHeight = "38px"; controlButton.style.margin = "8px 0 22px"; controlButton.style.padding = "0 5px"; controlButton.style.textAlign = "center"; controlButton.textContent = "Center Map"; controlButton.title = "Click to recenter the map"; controlButton.type = "button"; // Setup the click event listeners: simply set the map to Chicago. controlButton.addEventListener("click", () => { map.setCenter(chicago); }); return controlButton; } function initMap() { map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: chicago, }); // Create the DIV to hold the control. const centerControlDiv = document.createElement("div"); // Create the control. const centerControl = createCenterControl(map); // Append the control to the DIV. centerControlDiv.appendChild(centerControl); map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv); } window.initMap = initMap;
सैंपल आज़माएं
राज्य को कंट्रोल के तौर पर जोड़ा जा रहा है
कंट्रोल का स्टेटस भी सेव हो सकता है. जैसा कि पहले दिखाया गया है, नीचे दिए गए उदाहरण में कंट्रोल के तौर पर एक और "होम सेट करें" बटन दिया गया है. यह कंट्रोल, होम की नई जगह दिखाने के लिए कंट्रोल सेट करता है. हम ऐसा करने के लिए एक कंट्रोल में home_
प्रॉपर्टी बनाते हैं और उस स्थिति के लिए गैटर और सेटर सेट देते हैं.
TypeScript
let map: google.maps.Map; const chicago: google.maps.LatLngLiteral = { lat: 41.85, lng: -87.65 }; /** * The CenterControl adds a control to the map that recenters the map on * Chicago. */ class CenterControl { private map_: google.maps.Map; private center_: google.maps.LatLng; constructor( controlDiv: HTMLElement, map: google.maps.Map, center: google.maps.LatLngLiteral ) { this.map_ = map; // Set the center property upon construction this.center_ = new google.maps.LatLng(center); controlDiv.style.clear = "both"; // Set CSS for the control border const goCenterUI = document.createElement("button"); goCenterUI.id = "goCenterUI"; goCenterUI.title = "Click to recenter the map"; controlDiv.appendChild(goCenterUI); // Set CSS for the control interior const goCenterText = document.createElement("div"); goCenterText.id = "goCenterText"; goCenterText.innerHTML = "Center Map"; goCenterUI.appendChild(goCenterText); // Set CSS for the setCenter control border const setCenterUI = document.createElement("button"); setCenterUI.id = "setCenterUI"; setCenterUI.title = "Click to change the center of the map"; controlDiv.appendChild(setCenterUI); // Set CSS for the control interior const setCenterText = document.createElement("div"); setCenterText.id = "setCenterText"; setCenterText.innerHTML = "Set Center"; setCenterUI.appendChild(setCenterText); // Set up the click event listener for 'Center Map': Set the center of // the map // to the current center of the control. goCenterUI.addEventListener("click", () => { const currentCenter = this.center_; this.map_.setCenter(currentCenter); }); // Set up the click event listener for 'Set Center': Set the center of // the control to the current center of the map. setCenterUI.addEventListener("click", () => { const newCenter = this.map_.getCenter()!; if (newCenter) { this.center_ = newCenter; } }); } } function initMap(): void { map = new google.maps.Map(document.getElementById("map") as HTMLElement, { zoom: 12, center: chicago, }); // Create the DIV to hold the control and call the CenterControl() // constructor passing in this DIV. const centerControlDiv = document.createElement("div"); const control = new CenterControl(centerControlDiv, map, chicago); // @ts-ignore centerControlDiv.index = 1; centerControlDiv.style.paddingTop = "10px"; map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
let map; const chicago = { lat: 41.85, lng: -87.65 }; /** * The CenterControl adds a control to the map that recenters the map on * Chicago. */ class CenterControl { map_; center_; constructor(controlDiv, map, center) { this.map_ = map; // Set the center property upon construction this.center_ = new google.maps.LatLng(center); controlDiv.style.clear = "both"; // Set CSS for the control border const goCenterUI = document.createElement("button"); goCenterUI.id = "goCenterUI"; goCenterUI.title = "Click to recenter the map"; controlDiv.appendChild(goCenterUI); // Set CSS for the control interior const goCenterText = document.createElement("div"); goCenterText.id = "goCenterText"; goCenterText.innerHTML = "Center Map"; goCenterUI.appendChild(goCenterText); // Set CSS for the setCenter control border const setCenterUI = document.createElement("button"); setCenterUI.id = "setCenterUI"; setCenterUI.title = "Click to change the center of the map"; controlDiv.appendChild(setCenterUI); // Set CSS for the control interior const setCenterText = document.createElement("div"); setCenterText.id = "setCenterText"; setCenterText.innerHTML = "Set Center"; setCenterUI.appendChild(setCenterText); // Set up the click event listener for 'Center Map': Set the center of // the map // to the current center of the control. goCenterUI.addEventListener("click", () => { const currentCenter = this.center_; this.map_.setCenter(currentCenter); }); // Set up the click event listener for 'Set Center': Set the center of // the control to the current center of the map. setCenterUI.addEventListener("click", () => { const newCenter = this.map_.getCenter(); if (newCenter) { this.center_ = newCenter; } }); } } function initMap() { map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: chicago, }); // Create the DIV to hold the control and call the CenterControl() // constructor passing in this DIV. const centerControlDiv = document.createElement("div"); const control = new CenterControl(centerControlDiv, map, chicago); // @ts-ignore centerControlDiv.index = 1; centerControlDiv.style.paddingTop = "10px"; map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv); } window.initMap = initMap;