कस्टम ओवरले

प्लैटफ़ॉर्म चुनें: 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 के माध्यम से मैप पर जोड़ सकते हैं. API से पता चलता है कि ओवरले ओवरले के onAdd() तरीके को लागू करके मैप पर जोड़ा जा सकता है. मैनेज करने के लिए अपनी इमेज को होल्ड करने के लिए, <div> बनाने का तरीका. इसमें <img> एलिमेंट जोड़ें और उसे अटैच करें <div> से जोड़ें और फिर ओवरले को मैप के किसी पैन से जोड़ें. पैनल DOM ट्री के अंदर एक नोड है.

पैनल, टाइप के MapPanes, मैप पर अलग-अलग लेयर के लिए स्टैकिंग ऑर्डर नीचे दिए गए पैनल ये हैं साथ ही, जहां उन्हें नीचे से स्टैक किया गया हो. साथ ही, उन्हें क्रम से लगाया गया हो. ऊपर:

  • mapPane सबसे नीचे का पैनल है और यह टाइल के ऊपर है. शायद इसमें डीओएम न हो इवेंट. (पैन 0).
  • overlayLayer में पॉलीलाइन, पॉलीगॉन, ग्राउंड ओवरले, और टाइल लेयर है ओवरले करें. ऐसा हो सकता है कि इसमें डीओएम इवेंट न हों. (पैन 1).
  • markerLayer में मार्कर हैं. ऐसा हो सकता है कि इसमें डीओएम इवेंट न हों. (पैन 2).
  • overlayMouseTarget में डीओएम इवेंट पाने वाले एलिमेंट शामिल हैं. (पैन 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 पर ले जाते हैं, तो इससे ओवरले का 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;

सीएसएस

/* 
 * 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>

    <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 script 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>

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