บทนำ
An
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 init(): Promise<void> { // Import the needed libraries. const [{ InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), 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 InfoWindow({ headerContent: heading, content, ariaLabel: 'Uluru', maxWidth: 500, // Set max width (optional). }); // Create the marker. const marker = new 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, }); }); } void init();
JavaScript
// This example displays a marker at the center of Australia. // When the user clicks the marker, an info window opens. async function init() { // Import the needed libraries. const [{ InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), 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 InfoWindow({ headerContent: heading, content, ariaLabel: 'Uluru', maxWidth: 500, // Set max width (optional). }); // Create the marker. const marker = new 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, }); }); } void init();
ตัวอย่างต่อไปนี้จะตั้งค่า maxWidth ของหน้าต่างข้อมูล:
ดูตัวอย่าง.
ตั้งค่าโฟกัสบนหน้าต่างข้อมูล
หากต้องการตั้งค่าโฟกัสบนหน้าต่างข้อมูล ให้เรียกใช้เมธอด focus()
ของหน้าต่างข้อมูล พิจารณาใช้เมธอดนี้ร่วมกับเหตุการณ์ visible
ก่อนที่จะตั้งค่าโฟกัส การเรียกใช้เมธอดนี้ในหน้าต่างข้อมูลที่ไม่ปรากฏ
จะไม่มีผล เรียกใช้ open() เพื่อ
ให้หน้าต่างข้อมูลปรากฏ
ปิดหน้าต่างข้อมูล
โดยค่าเริ่มต้น หน้าต่างข้อมูลจะเปิดอยู่จนกว่าผู้ใช้จะคลิกตัวควบคุมการปิด (กากบาทที่ด้านขวาบนของหน้าต่างข้อมูล) หรือกดปุ่ม ESC
นอกจากนี้ คุณยังปิดหน้าต่างข้อมูลอย่างชัดเจนได้ด้วยการเรียกใช้เมธอด close()
ของหน้าต่างข้อมูล
เมื่อปิดหน้าต่างข้อมูล โฟกัสจะย้ายกลับไปที่องค์ประกอบที่โฟกัสอยู่ก่อนที่จะเปิดหน้าต่างข้อมูล หากองค์ประกอบดังกล่าวไม่พร้อมใช้งาน
โฟกัสจะย้ายกลับไปที่แผนที่ หากต้องการลบล้างลักษณะการทำงานนี้ คุณสามารถฟังเหตุการณ์ closeclick และจัดการโฟกัสด้วยตนเองตามที่แสดงในตัวอย่างต่อไปนี้
infoWindow.addListener('closeclick', ()=>{ // Handle focus manually. });
ย้ายหน้าต่างข้อมูล
คุณเปลี่ยนตำแหน่งของหน้าต่างข้อมูลได้ 2 วิธีดังนี้
- เรียกใช้
setPosition()ในหน้าต่างข้อมูล หรือ - แนบหน้าต่างข้อมูลกับเครื่องหมายใหม่โดยใช้เมธอด
InfoWindow.open()หมายเหตุ: หากคุณเรียกใช้open()โดยไม่ส่งเครื่องหมายInfoWindowจะใช้ตำแหน่ง ที่ระบุเมื่อสร้างผ่านInfoWindowOptionsออบเจ็กต์นิยามคู่