कंट्रोल की खास जानकारी
Maps JavaScript API की मदद से दिखाए गए मैप में यूज़र इंटरफ़ेस (यूआई) एलिमेंट होते हैं, ताकि उपयोगकर्ता, मैप के साथ इंटरैक्ट कर सकें. इन एलिमेंट को कंट्रोल कहा जाता है. अपने ऐप्लिकेशन में, अलग-अलग तरह के कंट्रोल शामिल किए जा सकते हैं. इसके अलावा, कुछ और न किया जा सकता है. साथ ही, Maps JavaScript API को सभी कंट्रोल बिहेवियर हैंडल करने दें.
नीचे दिया गया मैप, Maps JavaScript API से दिखाए गए कंट्रोल का डिफ़ॉल्ट सेट दिखाता है:
मैप में इस्तेमाल किए जा सकने वाले कंट्रोल के पूरे सेट की सूची नीचे दी गई है:
- ज़ूम कंट्रोल में, मैप का ज़ूम लेवल बदलने के लिए "+" और "-" बटन दिखते हैं. यह कंट्रोल डिफ़ॉल्ट रूप से, मैप के सबसे नीचे दाएं कोने में दिखता है.
- मैप टाइप कंट्रोल ड्रॉपडाउन
या हॉरिज़ॉन्टल बटन बार स्टाइल में उपलब्ध है. इससे उपयोगकर्ता मैप टाइप
(
ROADMAP
,SATELLITE
,HYBRID
याTERRAIN
) चुन सकते हैं. यह कंट्रोल, मैप के सबसे ऊपर बाएं कोने में डिफ़ॉल्ट रूप से दिखता है. - Street View कंट्रोल में पेगमैन आइकॉन होता है. इसे मैप पर खींचकर, Street View चालू किया जा सकता है. यह कंट्रोल डिफ़ॉल्ट रूप से, मैप पर सबसे नीचे दाईं ओर दिखता है.
- रोटेट करने का कंट्रोल, उन मैप के लिए झुकाने और घुमाने के विकल्प दिखाता है जिनमें तिरछी तस्वीरें होती हैं. यह कंट्रोल डिफ़ॉल्ट रूप से, मैप पर सबसे नीचे दाईं ओर दिखता है. ज़्यादा जानकारी के लिए, 45° वाली तस्वीरें देखें.
- स्केल कंट्रोल, मैप स्केल एलिमेंट दिखाता है. यह कंट्रोल डिफ़ॉल्ट रूप से बंद रहता है.
- फ़ुलस्क्रीन कंट्रोल की मदद से, मैप को फ़ुलस्क्रीन मोड में खोला जा सकता है. यह कंट्रोल, डेस्कटॉप और मोबाइल डिवाइसों पर डिफ़ॉल्ट रूप से चालू रहता है. ध्यान दें: iOS में फ़ुलस्क्रीन की सुविधा नहीं मिलती. इसलिए, iOS डिवाइसों पर फ़ुलस्क्रीन कंट्रोल नहीं दिखता.
- कीबोर्ड शॉर्टकट कंट्रोल में, मैप के साथ इंटरैक्ट करने के लिए कीबोर्ड शॉर्टकट की सूची दिखती है.
इन मैप कंट्रोल को सीधे तौर पर ऐक्सेस या इनमें बदलाव नहीं किया जा सकता. इसके बजाय, मैप के MapOptions
फ़ील्ड में बदलाव किया जाता है. इससे कंट्रोल के दिखने और दिखने पर असर पड़ता है. मैप के विकल्प बदलने के लिए,
अपना मैप इंस्टैंशिएट (सही MapOptions
के साथ) करने पर, कंट्रोल प्रज़ेंटेशन में बदलाव किया जा सकता है या
setOptions()
को कॉल करके, मैप के विकल्पों को डाइनैमिक तरीके से बदला जा
सकता है.
ये सभी कंट्रोल डिफ़ॉल्ट रूप से चालू नहीं होते. यूज़र इंटरफ़ेस (यूआई) के डिफ़ॉल्ट व्यवहार (और इस तरीके में बदलाव करने का तरीका) के बारे में जानने के लिए, नीचे दिया गया डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) देखें.
डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई)
मैप बहुत छोटा (200x200px) होने पर, डिफ़ॉल्ट रूप से सभी कंट्रोल गायब हो जाते हैं. कंट्रोल को साफ़ तौर पर, दिखने के लिए सेट करके, इस व्यवहार को बदला जा सकता है. मैप में कंट्रोल जोड़ना देखें.
ये कंट्रोल, मोबाइल और डेस्कटॉप डिवाइसों पर एक जैसे ही काम करते हैं. हालांकि, पूरी स्क्रीन पर एक जैसा ही कंट्रोल होता है. कंट्रोल की सूची में बताया गया तरीका देखें.
इसके अलावा, कीबोर्ड की हैंडलिंग की सुविधा, सभी डिवाइसों पर डिफ़ॉल्ट रूप से चालू रहती है.
डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) बंद किया जा रहा है
आपके पास एपीआई के डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) बटन को पूरी तरह से बंद करने का विकल्प होता है. ऐसा करने के लिए,
मैप की 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 }
मैप का साइज़ 200x200px से छोटा होने पर, डिफ़ॉल्ट रूप से सभी कंट्रोल हट जाते हैं.
कंट्रोल को साफ़ तौर पर, दिखने के लिए सेट करके, इस व्यवहार को बदला जा सकता है. उदाहरण के लिए, इस टेबल में दिखाया गया है कि मैप के साइज़ और 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
, पेगमैन कंट्रोल को चालू/बंद करता है, जिसकी मदद से उपयोगकर्ता Street View पैनोरामा चालू कर सकता है. डिफ़ॉल्ट रूप से, यह कंट्रोल दिखता है और मैप पर सबसे नीचे दाईं ओर दिखता है.streetViewControlOptions
फ़ील्ड, इस कंट्रोल के लिए इस्तेमाल किए जाने वालेStreetViewControlOptions
के बारे में भी बताता है.rotateControl
, 45° वाली इमेज का ओरिएंटेशन कंट्रोल करने के लिए, 'घुमाएं' कंट्रोल के दिखने का तरीका चालू/बंद करता है. डिफ़ॉल्ट रूप से, किसी मैप टाइप के लिए मौजूदा ज़ूम और जगह पर 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'
इवेंट के लिए एक लिसनर जोड़ता है. ध्यान दें कि यह इवेंट DOM से मिला है, न कि मैप से.
// 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
का कलेक्शन है. मैप में अपने हिसाब से कंट्रोल जोड़ने के लिए,
Node
(आम तौर पर, <div>
) को सही ControlPosition
में जोड़ा जाता है. (इन स्थितियों के बारे में जानकारी के लिए, ऊपर पोज़िशनिंग कंट्रोल करना देखें.)
हर 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;