कंट्रोल

प्लैटफ़ॉर्म चुनें: Android iOS JavaScript

कंट्रोल की खास जानकारी

Maps JavaScript API के ज़रिए दिखाए गए मैप में यूज़र इंटरफ़ेस (यूआई) एलिमेंट होते हैं, ताकि उपयोगकर्ता को मैप के साथ इंटरैक्शन किया जा सके. इन एलिमेंट को कंट्रोल कहा जाता है. आपके पास अपने ऐप्लिकेशन में इन कंट्रोल के अलग-अलग वैरिएंट शामिल करने का विकल्प होता है. इसके अलावा, आपको कुछ नहीं करना होगा. Maps JavaScript API को सभी कंट्रोल मैनेज करने की अनुमति देनी होगी.

नीचे दिया गया मैप, कंट्रोल का वह डिफ़ॉल्ट सेट दिखाता है जिसे Maps JavaScript API ने दिखाया है:

नीचे उन कंट्रोल के पूरे सेट की सूची दी गई है जिन्हें मैप में इस्तेमाल किया जा सकता है:

  • मैप का ज़ूम लेवल बदलने के लिए, ज़ूम कंट्रोल "+" और "-" बटन दिखाता है. यह कंट्रोल, मैप के सबसे नीचे दाएं कोने में डिफ़ॉल्ट रूप से दिखता है.
  • मैप टाइप कंट्रोल, ड्रॉपडाउन या हॉरिज़ॉन्टल बटन बार स्टाइल में उपलब्ध है, जिससे उपयोगकर्ता मैप का टाइप (ROADMAP, SATELLITE, HYBRID या TERRAIN) चुन सकते हैं. यह कंट्रोल डिफ़ॉल्ट रूप से मैप के ऊपरी बाएं कोने में दिखता है.
  • स्ट्रीट व्यू कंट्रोल में पेगमैन का आइकॉन होता है. इसे मैप पर खींचकर, Street View चालू किया जा सकता है. यह कंट्रोल मैप पर सबसे नीचे दाईं ओर डिफ़ॉल्ट रूप से दिखता है.
  • घुमाएं कंट्रोल में, तिरछी तस्वीरों वाले मैप के लिए झुकाने और घुमाने के विकल्प मिलते हैं. यह कंट्रोल, मैप पर सबसे नीचे दाईं ओर डिफ़ॉल्ट रूप से दिखता है. ज़्यादा जानकारी के लिए, 45° की तस्वीरें देखें.
  • स्केल कंट्रोल, मैप स्केल एलिमेंट दिखाता है. यह कंट्रोल डिफ़ॉल्ट रूप से बंद होता है.
  • फ़ुलस्क्रीन कंट्रोल की मदद से, मैप को फ़ुलस्क्रीन मोड में खोलने का विकल्प मिलता है. यह कंट्रोल, डेस्कटॉप और मोबाइल डिवाइसों पर डिफ़ॉल्ट रूप से चालू होता है. ध्यान दें: iOS पर फ़ुलस्क्रीन की सुविधा काम नहीं करती. इसलिए, iOS डिवाइसों पर फ़ुलस्क्रीन कंट्रोल नहीं दिखता.
  • कीबोर्ड शॉर्टकट कंट्रोल की मदद से, मैप के साथ इंटरैक्ट करने के लिए कीबोर्ड शॉर्टकट की सूची दिखती है.

इन मैप कंट्रोल को न तो ऐक्सेस किया जा सकता है और न ही इनमें बदलाव किया जा सकता है. इसके बजाय, मैप के MapOptions फ़ील्ड में बदलाव किया जाता है. इससे कंट्रोल के दिखने और उसे दिखाने पर असर पड़ता है. आपके पास, अपने मैप को इंस्टैंशिएट करते समय, कंट्रोल के प्रज़ेंटेशन में बदलाव करने का विकल्प है. इसके लिए, सही MapOptions का इस्तेमाल किया जा सकता है. इसके अलावा, मैप के विकल्पों को बदलने के लिए, setOptions() को कॉल करके, डाइनैमिक तरीके से मैप में बदलाव किया जा सकता है.

ये सभी कंट्रोल, डिफ़ॉल्ट रूप से चालू नहीं होते. यूआई के डिफ़ॉल्ट व्यवहार के बारे में जानने और इस तरह के काम करने के तरीके में बदलाव करने के बारे में जानने के लिए, नीचे डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई) देखें.

डिफ़ॉल्ट यूज़र इंटरफ़ेस (यूआई)

मैप बहुत छोटा (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 हां
200 x 200 पिक्सल 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 के बारे में भी बताया जाता है.
  • 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> एलिमेंट में रखें, ताकि आप अपने कंट्रोल को एक यूनिट के तौर पर इस्तेमाल कर सकें. हम नीचे दिखाए गए सैंपल में, इस डिज़ाइन पैटर्न का इस्तेमाल करेंगे.

आकर्षक कंट्रोल डिज़ाइन करने के लिए, सीएसएस और DOM स्ट्रक्चर की कुछ जानकारी होना ज़रूरी है. नीचे दिया गया कोड, बटन एलिमेंट बनाने के लिए एक फ़ंक्शन दिखाता है, जो मैप को शिकागो पर फ़ोकस करने के लिए पैन करता है.

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() का इस्तेमाल करें. यह काम करने वाले डीओएम इवेंट मैनेज करता है. यह कोड स्निपेट, ब्राउज़र के '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);

कस्टम कंट्रोल का उदाहरण

यह कंट्रोल आसान है. हालांकि, यह कुछ खास काम का नहीं है. साथ ही, इसमें ऊपर दिए गए पैटर्न को जोड़ा जा सकता है. यह कंट्रोल, मैप को किसी डिफ़ॉल्ट जगह पर सेंटर में लाकर, DOM '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;
उदाहरण देखें

सैंपल आज़माएं