การวางซ้อนพื้น

เลือกแพลตฟอร์ม: Android iOS JavaScript
  1. บทนำ
  2. เพิ่มการวางซ้อนพื้น
  3. นำการวางซ้อนพื้นออก

เกริ่นนำ

การวางซ้อนคือวัตถุบนแผนที่ที่เชื่อมโยงกับพิกัดละติจูด/ลองจิจูด จึงเคลื่อนที่ได้เมื่อคุณลากหรือซูมแผนที่ หากต้องการวางรูปภาพบนแผนที่ คุณใช้ออบเจ็กต์ GroundOverlay ได้

ดูข้อมูลเกี่ยวกับการวางซ้อนประเภทอื่นๆ ดู การวาดบนแผนที่

เพิ่มการวางซ้อนพื้น

เครื่องมือสร้างสำหรับ GroundOverlay จะระบุ URL ของอิมเมจและ LatLngBounds ของรูปภาพเป็นพารามิเตอร์ ภาพจะแสดงผลบนแผนที่โดยถูกจำกัดตามขอบเขตที่ระบุ และสอดคล้องโดยใช้การฉายภาพของแผนที่

TypeScript

// This example uses a GroundOverlay to place an image on the map
// showing an antique map of Newark, NJ.

let historicalOverlay;

function initMap(): void {
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 13,
      center: { lat: 40.74, lng: -74.18 },
    }
  );

  const imageBounds = {
    north: 40.773941,
    south: 40.712216,
    east: -74.12544,
    west: -74.22655,
  };

  historicalOverlay = new google.maps.GroundOverlay(
    "https://storage.googleapis.com/geo-devrel-public-buckets/newark_nj_1922-661x516.jpeg",
    imageBounds
  );
  historicalOverlay.setMap(map);
}

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

JavaScript

// This example uses a GroundOverlay to place an image on the map
// showing an antique map of Newark, NJ.
let historicalOverlay;

function initMap() {
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 13,
    center: { lat: 40.74, lng: -74.18 },
  });
  const imageBounds = {
    north: 40.773941,
    south: 40.712216,
    east: -74.12544,
    west: -74.22655,
  };

  historicalOverlay = new google.maps.GroundOverlay(
    "https://storage.googleapis.com/geo-devrel-public-buckets/newark_nj_1922-661x516.jpeg",
    imageBounds,
  );
  historicalOverlay.setMap(map);
}

window.initMap = initMap;
ดูตัวอย่าง

ลองใช้ตัวอย่าง

นำการวางซ้อนพื้นออก

หากต้องการนำการวางซ้อนออกจากแผนที่ ให้เรียกเมธอด setMap() ของการวางซ้อนที่ผ่าน null โปรดทราบว่าการเรียกใช้วิธีนี้จะไม่ลบการวางซ้อน แล้วนำการวางซ้อนออกจากแผนที่ แต่หากคุณต้องการลบการวางซ้อน คุณควรนำออกจากแผนที่แล้วตั้งค่าภาพซ้อนทับเป็น null

function removeOverlay() {
  historicalOverlay.setMap(null);
}

ดูตัวอย่าง