บทนำ
การวางซ้อนคือออบเจ็กต์บนแผนที่ที่เชื่อมโยงกับพิกัดละติจูด/ลองจิจูด ดังนั้นออบเจ็กต์จะเคลื่อนที่เมื่อคุณลากหรือซูมแผนที่ หากต้องการวางรูปภาพบนแผนที่ คุณสามารถใช้ออบเจ็กต์ 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); }