कस्टम ओवरले

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

शुरुआती जानकारी

ओवरले मैप पर ऐसे ऑब्जेक्ट होते हैं जो अक्षांश/देशांतर निर्देशांक से जुड़े होते हैं, ताकि मैप को खींचने या ज़ूम करने पर वे मूव हो जाएं. पहले से तय किए गए ओवरले टाइप के बारे में जानकारी पाने के लिए, मैप पर ड्रॉइंग बनाना देखें.

Maps JavaScript API, आपके कस्टम ओवरले बनाने के लिए OverlayView क्लास देता है. OverlayView एक बेस क्लास है. इससे ऐसे कई तरीके मिलते हैं जिन्हें ओवरले बनाते समय लागू करना ज़रूरी है. इस क्लास में कुछ ऐसे तरीके भी मौजूद हैं जिनकी मदद से स्क्रीन कोऑर्डिनेट और मैप पर मौजूद जगहों के बीच अनुवाद किया जा सकता है.

कस्टम ओवरले जोड़ें

कस्टम ओवरले बनाने के लिए ज़रूरी चरणों की खास जानकारी यहां दी गई है:

  • अपने कस्टम ओवरले ऑब्जेक्ट के prototype को google.maps.OverlayView() के नए इंस्टेंस पर सेट करें. इस तरह, यह ओवरले क्लास को सब-क्लास कर देगा.
  • अपने कस्टम ओवरले के लिए एक कंस्ट्रक्टर बनाएं और कोई भी शुरू करने वाला पैरामीटर सेट करें.
  • अपने प्रोटोटाइप में onAdd() तरीका लागू करें और ओवरले को मैप से अटैच करें. जब मैप ओवरले अटैच करने के लिए तैयार होगा, तब OverlayView.onAdd() को कॉल किया जाएगा.
  • अपने प्रोटोटाइप में draw() तरीका लागू करें और अपने ऑब्जेक्ट के विज़ुअल डिसप्ले को हैंडल करें. ऑब्जेक्ट पहली बार दिखने पर OverlayView.draw() को कॉल किया जाएगा.
  • ओवरले में जोड़े गए किसी भी एलिमेंट को हटाने के लिए आपको onRemove() वाला तरीका भी लागू करना चाहिए.

हर चरण के बारे में ज़्यादा जानकारी नीचे दी गई है. पूरा उदाहरण कोड देखा जा सकता है: उदाहरण कोड देखें.

ओवरले को सब-क्लास करें

नीचे दिए गए उदाहरण में, एक सामान्य इमेज ओवरले बनाने के लिए OverlayView का इस्तेमाल किया गया है.

अब हम USGSOverlay क्लास के लिए एक कंस्ट्रक्टर बनाते हैं और पास किए गए पैरामीटर को नए ऑब्जेक्ट की प्रॉपर्टी के तौर पर शुरू करते हैं.

TypeScript

/**
 * The custom USGSOverlay object contains the USGS image,
 * the bounds of the image, and a reference to the map.
 */
class USGSOverlay extends google.maps.OverlayView {
  private bounds: google.maps.LatLngBounds;
  private image: string;
  private div?: HTMLElement;

  constructor(bounds: google.maps.LatLngBounds, image: string) {
    super();

    this.bounds = bounds;
    this.image = image;
  }

JavaScript

/**
 * The custom USGSOverlay object contains the USGS image,
 * the bounds of the image, and a reference to the map.
 */
class USGSOverlay extends google.maps.OverlayView {
  bounds;
  image;
  div;
  constructor(bounds, image) {
    super();
    this.bounds = bounds;
    this.image = image;
  }

हम अभी इस ओवरले को ओवरले के कंस्ट्रक्टर में मैप से अटैच नहीं कर सकते. सबसे पहले, हमें यह पक्का करना होगा कि मैप के सभी पैनल उपलब्ध हों, क्योंकि वे मैप पर ऑब्जेक्ट के दिखने का क्रम तय करते हैं. एपीआई एक हेल्पर तरीका उपलब्ध कराता है, जिससे पता चलता है कि ऐसा हो चुका है. हम अगले सेक्शन में उस तरीके को बताएंगे.

ओवरले शुरू करें

जब ओवरले पहली बार इंस्टैंशिएट हो जाता है और दिखाने के लिए तैयार होता है, तो हमें उसे ब्राउज़र के DOM के ज़रिए मैप से अटैच करना होगा. एपीआई बताता है कि ओवरले को मैप में जोड़ा गया है. इसके लिए, ओवरले की onAdd() तरीके को चालू करें. इस तरीके का इस्तेमाल करने के लिए, हम इमेज को होल्ड करने के लिए <div> बनाते हैं, <img> एलिमेंट जोड़ते हैं, इसे <div> से अटैच करते हैं, और फिर ओवरले को मैप के किसी पैनल में जोड़ते हैं. पैनल, DOM ट्री में मौजूद एक नोड होता है.

MapPanes टाइप वाले पैनल, मैप पर अलग-अलग लेयर के लिए स्टैकिंग का क्रम तय करते हैं. नीचे दिए गए पैनल उपलब्ध हैं और उनकी गिनती उस क्रम में की गई है जिस क्रम में वे नीचे से ऊपर की ओर होते हैं:

  • mapPane सबसे नीचे का पैनल है और यह टाइल के ऊपर है. ऐसा हो सकता है कि इसे DOM इवेंट न मिलें. (पैन 0).
  • overlayLayer में पॉलीलाइन, पॉलीगॉन, ग्राउंड ओवरले और टाइल लेयर ओवरले होते हैं. ऐसा हो सकता है कि इसे DOM इवेंट न मिलें. (पैन 1).
  • markerLayer में मार्कर हैं. ऐसा हो सकता है कि इसे DOM इवेंट न मिलें. (पैन 2).
  • overlayMouseTarget में ऐसे एलिमेंट शामिल हैं जिन्हें DOM इवेंट मिलते हैं. (पैन 3).
  • floatPane में जानकारी विंडो होती है. यह सभी मैप ओवरले के ऊपर होता है. (पैन 4).

हमारी इमेज "ग्राउंड ओवरले" की तरह है, इसलिए हम overlayLayer पैनल का इस्तेमाल करेंगे. जब हमारे पास वह पैनल होगा, तो हम उसे बच्चे के तौर पर अपना ऑब्जेक्ट अटैच कर देंगे.

TypeScript

/**
 * onAdd is called when the map's panes are ready and the overlay has been
 * added to the map.
 */
onAdd() {
  this.div = document.createElement("div");
  this.div.style.borderStyle = "none";
  this.div.style.borderWidth = "0px";
  this.div.style.position = "absolute";

  // Create the img element and attach it to the div.
  const img = document.createElement("img");

  img.src = this.image;
  img.style.width = "100%";
  img.style.height = "100%";
  img.style.position = "absolute";
  this.div.appendChild(img);

  // Add the element to the "overlayLayer" pane.
  const panes = this.getPanes()!;

  panes.overlayLayer.appendChild(this.div);
}

JavaScript

/**
 * onAdd is called when the map's panes are ready and the overlay has been
 * added to the map.
 */
onAdd() {
  this.div = document.createElement("div");
  this.div.style.borderStyle = "none";
  this.div.style.borderWidth = "0px";
  this.div.style.position = "absolute";

  // Create the img element and attach it to the div.
  const img = document.createElement("img");

  img.src = this.image;
  img.style.width = "100%";
  img.style.height = "100%";
  img.style.position = "absolute";
  this.div.appendChild(img);

  // Add the element to the "overlayLayer" pane.
  const panes = this.getPanes();

  panes.overlayLayer.appendChild(this.div);
}

ओवरले बनाएं

ध्यान दें कि हमने ऊपर दिए गए कोड में कोई खास विज़ुअल डिसप्ले शुरू नहीं किया है. जब भी एपीआई को मैप पर ओवरले बनाने की ज़रूरत होती है, तब ओवरले पर एपीआई, एक अलग draw() तरीके को शुरू करता है. इसे पहली बार जोड़े जाने पर भी इस्तेमाल किया जाता है.

इसलिए, हम draw() के इस तरीके को लागू करेंगे. फिर, getProjection() का इस्तेमाल करके ओवरले के MapCanvasProjection को फिर से हासिल करेंगे. साथ ही, उन सटीक कोऑर्डिनेट का हिसाब लगाएंगे जिन पर ऑब्जेक्ट के सबसे ऊपर दाएं और नीचे बाएं पॉइंट को ऐंकर करना है. इसके बाद, हम <div> का साइज़ बदल सकते हैं. इससे ओवरले के कंस्ट्रक्टर में बताई गई सीमाओं के मुताबिक इमेज का साइज़ बदल जाएगा.

TypeScript

draw() {
  // We use the south-west and north-east
  // coordinates of the overlay to peg it to the correct position and size.
  // To do this, we need to retrieve the projection from the overlay.
  const overlayProjection = this.getProjection();

  // Retrieve the south-west and north-east coordinates of this overlay
  // in LatLngs and convert them to pixel coordinates.
  // We'll use these coordinates to resize the div.
  const sw = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getSouthWest()
  )!;
  const ne = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getNorthEast()
  )!;

  // Resize the image's div to fit the indicated dimensions.
  if (this.div) {
    this.div.style.left = sw.x + "px";
    this.div.style.top = ne.y + "px";
    this.div.style.width = ne.x - sw.x + "px";
    this.div.style.height = sw.y - ne.y + "px";
  }
}

JavaScript

draw() {
  // We use the south-west and north-east
  // coordinates of the overlay to peg it to the correct position and size.
  // To do this, we need to retrieve the projection from the overlay.
  const overlayProjection = this.getProjection();
  // Retrieve the south-west and north-east coordinates of this overlay
  // in LatLngs and convert them to pixel coordinates.
  // We'll use these coordinates to resize the div.
  const sw = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getSouthWest(),
  );
  const ne = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getNorthEast(),
  );

  // Resize the image's div to fit the indicated dimensions.
  if (this.div) {
    this.div.style.left = sw.x + "px";
    this.div.style.top = ne.y + "px";
    this.div.style.width = ne.x - sw.x + "px";
    this.div.style.height = sw.y - ne.y + "px";
  }
}

कस्टम ओवरले हटाना

हम मैप से ओवरले को सही तरीके से हटाने के लिए, onRemove() तरीका भी जोड़ते हैं.

TypeScript

/**
 * The onRemove() method will be called automatically from the API if
 * we ever set the overlay's map property to 'null'.
 */
onRemove() {
  if (this.div) {
    (this.div.parentNode as HTMLElement).removeChild(this.div);
    delete this.div;
  }
}

JavaScript

/**
 * The onRemove() method will be called automatically from the API if
 * we ever set the overlay's map property to 'null'.
 */
onRemove() {
  if (this.div) {
    this.div.parentNode.removeChild(this.div);
    delete this.div;
  }
}

कस्टम ओवरले को छिपाएं और दिखाएं

अगर किसी ओवरले को हटाने या बनाने के बजाय, छिपाना या दिखाना है, तो उसके दिखने की सेटिंग को अडजस्ट करने के लिए, hide() और show() वाले तरीके इस्तेमाल किए जा सकते हैं. इसके अलावा, मैप के DOM से ओवरले को अलग किया जा सकता है. हालांकि, यह कार्रवाई थोड़ी ज़्यादा महंगी है. ध्यान दें कि अगर आप ओवरले को मैप के DOM से फिर से अटैच करते हैं, तो यह ओवरले की onAdd() तरीके को फिर से लागू कर देगा.

नीचे दिए गए उदाहरण में, ओवरले के प्रोटोटाइप में hide() और show() तरीके जोड़े गए हैं, जो कंटेनर <div> के दिखने की सेटिंग को टॉगल करते हैं. इसके अलावा, हम toggleDOM() वाला तरीका जोड़ते हैं. यह तरीका, ओवरले को मैप से जोड़ता या अलग करता है.

TypeScript

/**
 *  Set the visibility to 'hidden' or 'visible'.
 */
hide() {
  if (this.div) {
    this.div.style.visibility = "hidden";
  }
}

show() {
  if (this.div) {
    this.div.style.visibility = "visible";
  }
}

toggle() {
  if (this.div) {
    if (this.div.style.visibility === "hidden") {
      this.show();
    } else {
      this.hide();
    }
  }
}

toggleDOM(map: google.maps.Map) {
  if (this.getMap()) {
    this.setMap(null);
  } else {
    this.setMap(map);
  }
}

JavaScript

/**
 *  Set the visibility to 'hidden' or 'visible'.
 */
hide() {
  if (this.div) {
    this.div.style.visibility = "hidden";
  }
}
show() {
  if (this.div) {
    this.div.style.visibility = "visible";
  }
}
toggle() {
  if (this.div) {
    if (this.div.style.visibility === "hidden") {
      this.show();
    } else {
      this.hide();
    }
  }
}
toggleDOM(map) {
  if (this.getMap()) {
    this.setMap(null);
  } else {
    this.setMap(map);
  }
}

बटन कंट्रोल जोड़ें

toggle और toggleDom तरीकों को ट्रिगर करने के लिए, मैप में बटन को कंट्रोल किया जाता है.

TypeScript

const toggleButton = document.createElement("button");

toggleButton.textContent = "Toggle";
toggleButton.classList.add("custom-map-control-button");

const toggleDOMButton = document.createElement("button");

toggleDOMButton.textContent = "Toggle DOM Attachment";
toggleDOMButton.classList.add("custom-map-control-button");

toggleButton.addEventListener("click", () => {
  overlay.toggle();
});

toggleDOMButton.addEventListener("click", () => {
  overlay.toggleDOM(map);
});

map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);

JavaScript

const toggleButton = document.createElement("button");

toggleButton.textContent = "Toggle";
toggleButton.classList.add("custom-map-control-button");

const toggleDOMButton = document.createElement("button");

toggleDOMButton.textContent = "Toggle DOM Attachment";
toggleDOMButton.classList.add("custom-map-control-button");
toggleButton.addEventListener("click", () => {
  overlay.toggle();
});
toggleDOMButton.addEventListener("click", () => {
  overlay.toggleDOM(map);
});
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);

सैंपल कोड पूरा करें

पूरा सैंपल कोड नीचे दिया गया है:

TypeScript

// This example adds hide() and show() methods to a custom overlay's prototype.
// These methods toggle the visibility of the container <div>.
// overlay to or from the map.

function initMap(): void {
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 11,
      center: { lat: 62.323907, lng: -150.109291 },
      mapTypeId: "satellite",
    }
  );

  const bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(62.281819, -150.287132),
    new google.maps.LatLng(62.400471, -150.005608)
  );

  // The photograph is courtesy of the U.S. Geological Survey.
  let image = "https://developers.google.com/maps/documentation/javascript/";

  image += "examples/full/images/talkeetna.png";

  /**
   * The custom USGSOverlay object contains the USGS image,
   * the bounds of the image, and a reference to the map.
   */
  class USGSOverlay extends google.maps.OverlayView {
    private bounds: google.maps.LatLngBounds;
    private image: string;
    private div?: HTMLElement;

    constructor(bounds: google.maps.LatLngBounds, image: string) {
      super();

      this.bounds = bounds;
      this.image = image;
    }

    /**
     * onAdd is called when the map's panes are ready and the overlay has been
     * added to the map.
     */
    onAdd() {
      this.div = document.createElement("div");
      this.div.style.borderStyle = "none";
      this.div.style.borderWidth = "0px";
      this.div.style.position = "absolute";

      // Create the img element and attach it to the div.
      const img = document.createElement("img");

      img.src = this.image;
      img.style.width = "100%";
      img.style.height = "100%";
      img.style.position = "absolute";
      this.div.appendChild(img);

      // Add the element to the "overlayLayer" pane.
      const panes = this.getPanes()!;

      panes.overlayLayer.appendChild(this.div);
    }

    draw() {
      // We use the south-west and north-east
      // coordinates of the overlay to peg it to the correct position and size.
      // To do this, we need to retrieve the projection from the overlay.
      const overlayProjection = this.getProjection();

      // Retrieve the south-west and north-east coordinates of this overlay
      // in LatLngs and convert them to pixel coordinates.
      // We'll use these coordinates to resize the div.
      const sw = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getSouthWest()
      )!;
      const ne = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getNorthEast()
      )!;

      // Resize the image's div to fit the indicated dimensions.
      if (this.div) {
        this.div.style.left = sw.x + "px";
        this.div.style.top = ne.y + "px";
        this.div.style.width = ne.x - sw.x + "px";
        this.div.style.height = sw.y - ne.y + "px";
      }
    }

    /**
     * The onRemove() method will be called automatically from the API if
     * we ever set the overlay's map property to 'null'.
     */
    onRemove() {
      if (this.div) {
        (this.div.parentNode as HTMLElement).removeChild(this.div);
        delete this.div;
      }
    }

    /**
     *  Set the visibility to 'hidden' or 'visible'.
     */
    hide() {
      if (this.div) {
        this.div.style.visibility = "hidden";
      }
    }

    show() {
      if (this.div) {
        this.div.style.visibility = "visible";
      }
    }

    toggle() {
      if (this.div) {
        if (this.div.style.visibility === "hidden") {
          this.show();
        } else {
          this.hide();
        }
      }
    }

    toggleDOM(map: google.maps.Map) {
      if (this.getMap()) {
        this.setMap(null);
      } else {
        this.setMap(map);
      }
    }
  }

  const overlay: USGSOverlay = new USGSOverlay(bounds, image);

  overlay.setMap(map);

  const toggleButton = document.createElement("button");

  toggleButton.textContent = "Toggle";
  toggleButton.classList.add("custom-map-control-button");

  const toggleDOMButton = document.createElement("button");

  toggleDOMButton.textContent = "Toggle DOM Attachment";
  toggleDOMButton.classList.add("custom-map-control-button");

  toggleButton.addEventListener("click", () => {
    overlay.toggle();
  });

  toggleDOMButton.addEventListener("click", () => {
    overlay.toggleDOM(map);
  });

  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

JavaScript

// This example adds hide() and show() methods to a custom overlay's prototype.
// These methods toggle the visibility of the container <div>.
// overlay to or from the map.
function initMap() {
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 11,
    center: { lat: 62.323907, lng: -150.109291 },
    mapTypeId: "satellite",
  });
  const bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(62.281819, -150.287132),
    new google.maps.LatLng(62.400471, -150.005608),
  );
  // The photograph is courtesy of the U.S. Geological Survey.
  let image = "https://developers.google.com/maps/documentation/javascript/";

  image += "examples/full/images/talkeetna.png";
  /**
   * The custom USGSOverlay object contains the USGS image,
   * the bounds of the image, and a reference to the map.
   */
  class USGSOverlay extends google.maps.OverlayView {
    bounds;
    image;
    div;
    constructor(bounds, image) {
      super();
      this.bounds = bounds;
      this.image = image;
    }
    /**
     * onAdd is called when the map's panes are ready and the overlay has been
     * added to the map.
     */
    onAdd() {
      this.div = document.createElement("div");
      this.div.style.borderStyle = "none";
      this.div.style.borderWidth = "0px";
      this.div.style.position = "absolute";

      // Create the img element and attach it to the div.
      const img = document.createElement("img");

      img.src = this.image;
      img.style.width = "100%";
      img.style.height = "100%";
      img.style.position = "absolute";
      this.div.appendChild(img);

      // Add the element to the "overlayLayer" pane.
      const panes = this.getPanes();

      panes.overlayLayer.appendChild(this.div);
    }
    draw() {
      // We use the south-west and north-east
      // coordinates of the overlay to peg it to the correct position and size.
      // To do this, we need to retrieve the projection from the overlay.
      const overlayProjection = this.getProjection();
      // Retrieve the south-west and north-east coordinates of this overlay
      // in LatLngs and convert them to pixel coordinates.
      // We'll use these coordinates to resize the div.
      const sw = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getSouthWest(),
      );
      const ne = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getNorthEast(),
      );

      // Resize the image's div to fit the indicated dimensions.
      if (this.div) {
        this.div.style.left = sw.x + "px";
        this.div.style.top = ne.y + "px";
        this.div.style.width = ne.x - sw.x + "px";
        this.div.style.height = sw.y - ne.y + "px";
      }
    }
    /**
     * The onRemove() method will be called automatically from the API if
     * we ever set the overlay's map property to 'null'.
     */
    onRemove() {
      if (this.div) {
        this.div.parentNode.removeChild(this.div);
        delete this.div;
      }
    }
    /**
     *  Set the visibility to 'hidden' or 'visible'.
     */
    hide() {
      if (this.div) {
        this.div.style.visibility = "hidden";
      }
    }
    show() {
      if (this.div) {
        this.div.style.visibility = "visible";
      }
    }
    toggle() {
      if (this.div) {
        if (this.div.style.visibility === "hidden") {
          this.show();
        } else {
          this.hide();
        }
      }
    }
    toggleDOM(map) {
      if (this.getMap()) {
        this.setMap(null);
      } else {
        this.setMap(map);
      }
    }
  }

  const overlay = new USGSOverlay(bounds, image);

  overlay.setMap(map);

  const toggleButton = document.createElement("button");

  toggleButton.textContent = "Toggle";
  toggleButton.classList.add("custom-map-control-button");

  const toggleDOMButton = document.createElement("button");

  toggleDOMButton.textContent = "Toggle DOM Attachment";
  toggleDOMButton.classList.add("custom-map-control-button");
  toggleButton.addEventListener("click", () => {
    overlay.toggle();
  });
  toggleDOMButton.addEventListener("click", () => {
    overlay.toggleDOM(map);
  });
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);
}

window.initMap = initMap;

CSS

/* 
 * Always set the map height explicitly to define the size of the div element
 * that contains the map. 
 */
#map {
  height: 100%;
}

/* 
 * Optional: Makes the sample page fill the window. 
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.custom-map-control-button {
  background-color: #fff;
  border: 0;
  border-radius: 2px;
  box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
  margin: 10px;
  padding: 0 0.5em;
  font: 400 18px Roboto, Arial, sans-serif;
  overflow: hidden;
  height: 40px;
  cursor: pointer;
}
.custom-map-control-button:hover {
  background: rgb(235, 235, 235);
}

एचटीएमएल

<html>
  <head>
    <title>Showing/Hiding Overlays</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="map"></div>

    <!-- 
      The `defer` attribute causes the callback to execute after the full HTML
      document has been parsed. For non-blocking uses, avoiding race conditions,
      and consistent behavior across browsers, consider loading using Promises.
      See https://developers.google.com/maps/documentation/javascript/load-maps-js-api
      for more information.
      -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly"
      defer
    ></script>
  </body>
</html>

नमूना आज़माएं