บทนำ
InfoWindow จะแสดงเนื้อหา (โดยปกติคือข้อความหรือรูปภาพ) ใน
หน้าต่างป๊อปอัปเหนือแผนที่ ณ สถานที่ที่ระบุ หน้าต่างข้อมูลมีพื้นที่เนื้อหา
และก้านเรียว ปลายก้านจะติดอยู่กับตำแหน่งที่ระบุ
บนแผนที่ หน้าต่างข้อมูลจะปรากฏเป็นกล่องโต้ตอบในโปรแกรมอ่านหน้าจอ
โดยปกติแล้ว คุณจะแนบหน้าต่างข้อมูลกับเครื่องหมาย แต่คุณยังแนบหน้าต่างข้อมูลกับละติจูด/ลองจิจูดที่เฉพาะเจาะจงได้ด้วย ดังที่อธิบายไว้ในส่วนการเพิ่มหน้าต่างข้อมูลด้านล่าง
โดยทั่วไปแล้ว หน้าต่างข้อมูลเป็นโฆษณาซ้อนทับประเภทหนึ่ง ดูข้อมูลเกี่ยวกับ การซ้อนทับประเภทอื่นๆ ได้ที่ การวาดบนแผนที่
เพิ่มหน้าต่างข้อมูล
ตัวสร้าง
InfoWindow จะใช้อักษรสัญพจน์ออบเจ็กต์
InfoWindowOptions ซึ่งระบุพารามิเตอร์เริ่มต้น
สำหรับการแสดงหน้าต่างข้อมูล
ออบเจ็กต์ลิเทอรัล InfoWindowOptions
มีช่องต่อไปนี้
contentมีสตริงข้อความหรือโหนด DOM เพื่อแสดงในหน้าต่างข้อมูลpixelOffsetมีออฟเซ็ตจากปลายของหน้าต่างข้อมูล ไปยังตำแหน่งที่หน้าต่างข้อมูลยึดไว้ ในทางปฏิบัติ คุณไม่จำเป็นต้องระบุฟิลด์นี้ คุณสามารถปล่อยให้เป็นค่าเริ่มต้นได้positionมีLatLngที่ยึดหน้าต่างข้อมูลนี้ ไว้ หมายเหตุ:InfoWindowอาจแนบกับMarkerออบเจ็กต์ (ในกรณีนี้ตำแหน่งจะอิงตามตำแหน่งของเครื่องหมาย) หรือบนแผนที่เองที่LatLngที่ระบุ วิธีหนึ่งในการดึงข้อมูลLatLngคือการใช้ บริการเข้ารหัสพิกัดภูมิศาสตร์ การเปิดหน้าต่างข้อมูลในเครื่องหมายจะอัปเดตpositionโดยอัตโนมัติmaxWidthระบุความกว้างสูงสุดของหน้าต่างข้อมูล เป็นพิกเซล โดยค่าเริ่มต้น หน้าต่างข้อมูลจะขยายให้พอดีกับเนื้อหา และจะตัดข้อความโดยอัตโนมัติหากหน้าต่างข้อมูลเต็มแผนที่ หากคุณ เพิ่มmaxWidthหน้าต่างข้อมูลจะตัดข้อความโดยอัตโนมัติเพื่อบังคับใช้ ความกว้างที่ระบุ หากถึงความกว้างสูงสุดและมีพื้นที่แนวตั้งบนหน้าจอ หน้าต่างข้อมูลอาจขยายในแนวตั้ง
เนื้อหาของ InfoWindow อาจมีสตริงข้อความ
ข้อมูลโค้ด HTML หรือองค์ประกอบ DOM หากต้องการตั้งค่าเนื้อหา ให้ระบุเนื้อหาภายใน InfoWindowOptions หรือเรียกใช้ setContent() ใน InfoWindow อย่างชัดเจน
หากต้องการกำหนดขนาดเนื้อหาอย่างชัดเจน คุณสามารถใส่เนื้อหาในองค์ประกอบ
<div> และจัดรูปแบบ <div> ด้วย
CSS คุณใช้ CSS เพื่อเปิดใช้การเลื่อนได้ด้วย โปรดทราบว่าหากคุณไม่
เปิดใช้การเลื่อนและเนื้อหามีขนาดเกินพื้นที่ที่มีในหน้าต่างข้อมูล
เนื้อหาอาจล้นออกมาจากหน้าต่างข้อมูล
เปิดหน้าต่างข้อมูล
เมื่อสร้างหน้าต่างข้อมูล หน้าต่างดังกล่าวจะไม่แสดงบนแผนที่โดยอัตโนมัติ
หากต้องการให้หน้าต่างข้อมูลปรากฏ คุณต้องเรียกใช้เมธอด open()
ใน InfoWindow โดยส่งออบเจ็กต์ลิเทอรัล InfoWindowOpenOptions
ที่ระบุตัวเลือกต่อไปนี้
mapระบุแผนที่หรือพาโนรามาใน Street View ที่จะเปิดanchorมีจุดยึด (เช่นMarker) หากตัวเลือกanchorเป็นnullหรือไม่ได้กำหนดไว้ หน้าต่างข้อมูล จะเปิดขึ้นที่พร็อพเพอร์ตี้position
TypeScript
// This example displays a marker at the center of Australia. // When the user clicks the marker, an info window opens. async function initMap(): Promise<void> { // Import the needed libraries. await google.maps.importLibrary("maps"); await google.maps.importLibrary("marker"); // Get the map element and the inner map from it. const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; const innerMap = mapElement.innerMap; // Get the center of the map. const center = mapElement.center; // Create the info window content. const heading = document.createElement("h1"); heading.textContent = "Uluru (Ayers Rock)"; const content = document.createElement("div"); const infoParagraph = document.createElement("p"); infoParagraph.textContent = `Uluru, also referred to as Ayers Rock, is a large sandstone rock formation in the southern part of the Northern Territory, central Australia. It lies 335 km (208 mi) south west of the nearest large town, Alice Springs; 450 km (280 mi) by road. Kata Tjuta and Uluru are the two major features of the Uluru - Kata Tjuta National Park. Uluru is sacred to the Pitjantjatjara and Yankunytjatjara, the Aboriginal people of the area. It has many springs, waterholes, rock caves and ancient paintings. Uluru is listed as a World Heritage Site.`; content.appendChild(infoParagraph); const link = document.createElement("a"); link.href = "https://en.wikipedia.org/w/index.php?title=Uluru"; link.textContent = "https://en.wikipedia.org/w/index.php?title=Uluru"; link.target = "_blank"; content.appendChild(link); // Create the info window. const infowindow = new google.maps.InfoWindow({ headerContent: heading, content: content, ariaLabel: "Uluru", maxWidth: 500, // Set max width (optional). }); // Create the marker. const marker = new google.maps.marker.AdvancedMarkerElement({ position: center, map: innerMap, title: "Uluru (Ayers Rock)", gmpClickable: true, }); // Open the info window when the map loads. infowindow.open({ anchor: marker, map: innerMap, }); // Open the info window when the marker is clicked. marker.addEventListener("gmp-click", () => { infowindow.open({ anchor: marker, map: innerMap, }); }); } initMap();
JavaScript
// This example displays a marker at the center of Australia. // When the user clicks the marker, an info window opens. async function initMap() { // Import the needed libraries. await google.maps.importLibrary("maps"); await google.maps.importLibrary("marker"); // Get the map element and the inner map from it. const mapElement = document.querySelector('gmp-map'); const innerMap = mapElement.innerMap; // Get the center of the map. const center = mapElement.center; // Create the info window content. const heading = document.createElement("h1"); heading.textContent = "Uluru (Ayers Rock)"; const content = document.createElement("div"); const infoParagraph = document.createElement("p"); infoParagraph.textContent = `Uluru, also referred to as Ayers Rock, is a large sandstone rock formation in the southern part of the Northern Territory, central Australia. It lies 335 km (208 mi) south west of the nearest large town, Alice Springs; 450 km (280 mi) by road. Kata Tjuta and Uluru are the two major features of the Uluru - Kata Tjuta National Park. Uluru is sacred to the Pitjantjatjara and Yankunytjatjara, the Aboriginal people of the area. It has many springs, waterholes, rock caves and ancient paintings. Uluru is listed as a World Heritage Site.`; content.appendChild(infoParagraph); const link = document.createElement("a"); link.href = "https://en.wikipedia.org/w/index.php?title=Uluru"; link.textContent = "https://en.wikipedia.org/w/index.php?title=Uluru"; link.target = "_blank"; content.appendChild(link); // Create the info window. const infowindow = new google.maps.InfoWindow({ headerContent: heading, content: content, ariaLabel: "Uluru", maxWidth: 500, // Set max width (optional). }); // Create the marker. const marker = new google.maps.marker.AdvancedMarkerElement({ position: center, map: innerMap, title: "Uluru (Ayers Rock)", gmpClickable: true, }); // Open the info window when the map loads. infowindow.open({ anchor: marker, map: innerMap, }); // Open the info window when the marker is clicked. marker.addEventListener("gmp-click", () => { infowindow.open({ anchor: marker, map: innerMap, }); }); } initMap();
ลองใช้ตัวอย่าง
ตัวอย่างต่อไปนี้จะตั้งค่า maxWidth ของหน้าต่างข้อมูล
ดูตัวอย่าง
โฟกัสที่หน้าต่างข้อมูล
หากต้องการตั้งค่าโฟกัสในหน้าต่างข้อมูล ให้เรียกใช้เมธอด focus()
ลองใช้วิธีนี้ร่วมกับเหตุการณ์ visible
ก่อนตั้งค่าโฟกัส การเรียกใช้เมธอดนี้ในหน้าต่างข้อมูลที่ไม่แสดงจะไม่มีผล เรียกใช้ open() เพื่อ
ทำให้หน้าต่างข้อมูลปรากฏ
ปิดหน้าต่างข้อมูล
โดยค่าเริ่มต้น หน้าต่างข้อมูลจะเปิดอยู่จนกว่าผู้ใช้จะคลิกตัวควบคุมปิด (กากบาทที่ด้านขวาบนของหน้าต่างข้อมูล) หรือกดปุ่ม ESC
นอกจากนี้ คุณยังปิดหน้าต่างข้อมูลอย่างชัดเจนได้โดยเรียกใช้เมธอด close()
เมื่อปิดหน้าต่างข้อมูล โฟกัสจะย้ายกลับไปยังองค์ประกอบที่อยู่ใน
โฟกัสก่อนที่จะเปิดหน้าต่างข้อมูล หากองค์ประกอบนั้นไม่พร้อมใช้งาน
ระบบจะย้ายโฟกัสกลับไปที่แผนที่ หากต้องการลบล้างลักษณะการทํางานนี้ คุณสามารถรอรับ
เหตุการณ์ closeclick และจัดการโฟกัสด้วยตนเองได้ดังที่แสดงใน
ตัวอย่างต่อไปนี้
infoWindow.addListener('closeclick', ()=>{ // Handle focus manually. });
ย้ายหน้าต่างข้อมูล
คุณเปลี่ยนตำแหน่งของหน้าต่างข้อมูลได้ 2 วิธีดังนี้
- โทรหา
setPosition()ในหน้าต่างข้อมูล หรือ - แนบหน้าต่างข้อมูลกับเครื่องหมายใหม่โดยใช้เมธอด
InfoWindow.open()หมายเหตุ: หากคุณเรียกใช้open()โดยไม่ส่งเครื่องหมายInfoWindowจะใช้ตำแหน่ง ที่ระบุเมื่อสร้างผ่านออบเจ็กต์ลิเทอรัลInfoWindowOptions
การปรับแต่ง
InfoWindow ไม่มีการปรับแต่ง แต่ให้ดู
ตัวอย่างป๊อปอัปที่ปรับแต่งแล้ว
เพื่อดูวิธีสร้างป๊อปอัปที่ปรับแต่งอย่างเต็มรูปแบบแทน