कंट्रोल

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

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

Maps JavaScript API के ज़रिए दिखाए गए मैप में यूज़र इंटरफ़ेस (यूआई) होता है एलिमेंट का इस्तेमाल करें, ताकि उपयोगकर्ता मैप पर इंटरैक्शन कर सके. इन एलिमेंट को यह कहा जाता है: controls का इस्तेमाल करें और आप का इस्तेमाल करें. वैकल्पिक रूप से, आप कुछ नहीं कर सकते 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 पर दिखाए गए हॉरिज़ॉन्टल बार में बटन के तौर पर कंट्रोल का कलेक्शन मैप.
  • 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> एलिमेंट जोड़ें, ताकि आप अपने कंट्रोल में एक इकाई होगी. हम नीचे दिखाए गए सैंपल में, इस डिज़ाइन पैटर्न का इस्तेमाल करेंगे.

आकर्षक कंट्रोल डिज़ाइन करने के लिए, CSS और 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. आप Node (आम तौर पर <div>) जोड़कर मैप करें ControlPosition पर सेट करना होगा. (इनकी जानकारी के लिए स्थिति के लिए, स्थिति-निर्धारण नियंत्रण देखें above.)

हर 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;
अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
उदाहरण देखें

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