ป๊อปอัป

popover จะแสดงเนื้อหา (โดยปกติคือข้อความหรือรูปภาพ) ในหน้าต่างโต้ตอบเหนือแผนที่ ณ ตำแหน่งหนึ่งๆ ข้อความป๊อปอัปมีพื้นที่เนื้อหาและก้านเรียว โดยปลายก้านจะแนบอยู่กับตำแหน่งที่ระบุบนแผนที่ กล่องป๊อปอัปจะปรากฏเป็นกล่องโต้ตอบต่อโปรแกรมอ่านหน้าจอ

โดยปกติแล้วคุณจะแนบป๊อปอัปกับเครื่องหมายแบบโต้ตอบ แต่คุณยังแนบป๊อปอัปกับพิกัด LatLngAltitude ที่เฉพาะเจาะจง หรือถ่วงดุลจากเครื่องหมายได้ด้วย

เพิ่มป๊อปอัป

ตัวสร้างของ PopoverElement จะรับค่าลิเทอรัลออบเจ็กต์ PopoverElementOptions ซึ่งระบุพารามิเตอร์เริ่มต้นสำหรับการแสดงป๊อปอัป

ออบเจ็กต์ลิเทอรัล PopoverElementOptions มีช่องต่อไปนี้

  • positionAnchor: ตำแหน่ง LatLngAltitude ที่แสดงเครื่องหมาย หากใช้เครื่องหมาย ระบบจะใช้ตำแหน่งของเครื่องหมายแทน
  • altitudeMode: วิธีตีความระดับความสูงของป๊อปอัป
  • lightDismissDisabled: กำหนดว่าป๊อปอัปจะยังคงเปิดอยู่หรือไม่เมื่อผู้ใช้คลิกนอกป๊อปอัปหรือกดแป้น Esc เมื่อตั้งค่าตัวเลือกนี้เป็น true ระบบจะแสดงป๊อปอัป lightDismissDisabled หลายรายการบนแผนที่พร้อมกันได้
  • open: กำหนดว่าควรเปิดป๊อปอัปหรือไม่ ค่าเริ่มต้นคือ false

เนื้อหาของ PopoverElement อาจมีสตริงข้อความ ข้อมูลโค้ด HTML หรือองค์ประกอบ DOM คุณตั้งค่าเนื้อหาได้โดยใส่ต่อท้าย PopoverElement อย่างชัดเจนในช่อง header หรือ default

หากต้องการกำหนดขนาดเนื้อหาอย่างชัดเจน ให้ใส่เนื้อหาไว้ในองค์ประกอบ <div> แล้วจัดสไตล์ <div> ด้วย CSS คุณสามารถเลื่อนป๊อปอัปได้โดยค่าเริ่มต้น และมีการกำหนดความสูงสูงสุดไว้ล่วงหน้า

ยึดป๊อปอัปกับพิกัด LatLngAltitude

เมื่อคุณสร้างป๊อปอัป ป๊อปอัปจะไม่แสดงบนแผนที่โดยอัตโนมัติ หากต้องการให้ป๊อปอัปแสดง คุณต้องตั้งค่าตัวเลือก open เป็น true ใน PopoverElement คุณดำเนินการนี้ในระหว่างการสร้างหรือหลังจากการสร้างอินสแตนซ์แล้วได้

async function init() {
    const { AltitudeMode, Map3DElement, MapMode, PopoverElement } = await google.maps.importLibrary("maps3d");
    const map = new Map3DElement({
        center: { lat: 37.8204, lng: -122.4783, altitude: 0.407 }, range: 4000, tilt: 74, heading: 38,
        mode: MapMode.HYBRID,
    });
    const popover = new PopoverElement({
        altitudeMode: AltitudeMode.ABSOLUTE,
        open: true,
        positionAnchor: { lat: 37.819852, lng: -122.478549, altitude: 150 },
    });
    popover.append('Golden Gate Bridge');
    map.append(popover);
    document.body.append(map);
}
init();

ยึดป๊อปอัปกับเครื่องหมาย

คุณยึดพอพเพอร์อัปกับเครื่องหมายได้ เมื่อเพิ่มป๊อปอัปที่ยึดกับเครื่องหมาย คุณจะต้องตั้งค่าตัวเลือก PopoverElement.positionAnchor เพื่อใช้เครื่องหมาย

async function init() {
    const { AltitudeMode, Map3DElement, Marker3DInteractiveElement, MapMode, PopoverElement } = await google.maps.importLibrary("maps3d");
    const map = new Map3DElement({
        center: { lat: 37.8204, lng: -122.4783, altitude: 0.407 }, range: 4000, tilt: 74, heading: 38,
        mode: MapMode.HYBRID,
    });
    // Popovers can only be added to interactive Markers
    const interactiveMarker = new Marker3DInteractiveElement({
        altitudeMode: AltitudeMode.ABSOLUTE,
        position: { lat: 37.819852, lng: -122.478549, altitude: 100 }
    });
    const popover = new PopoverElement({
        open: false,
        positionAnchor: interactiveMarker,
    });
    popover.append('Golden Gate Bridge');
    interactiveMarker.addEventListener('gmp-click', (event) => {
        // toggle the marker to the other state (unlee you are clicking on the marker itself when it reopens it)
        popover.open = !popover.open;
    });
    map.append(interactiveMarker);
    map.append(popover);
    document.body.append(map);
}
init();

ยึดพอปอัปกับเครื่องหมายโดยใช้ HTML

นอกจากนี้ คุณยังยึดป๊อปอัปกับเครื่องหมายได้โดยไม่ต้องเขียนโค้ด JavaScript ใดๆ ตามที่แสดงด้านล่าง

<gmp-map-3d mode="hybrid" center="37.819852,-122.478549" tilt="75" range="2000" heading="330">
  <gmp-marker-3d-interactive gmp-popover-target="my-popover" position="37.819852,-122.478549,100"></gmp-marker-3d-interactive>
  <gmp-popover id="my-popover">
    Golden Gate Bridge
  </gmp-popover>
</gmp-map-3d>
<script async src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDGl0teQ6qwaBEdHazOWIEXd8XGWOZvoaM&v=beta&libraries=maps3d">
</script>

เพิ่มเนื้อหาที่กําหนดเองลงในป๊อปอัป

คุณเพิ่มเนื้อหาที่กำหนดเองลงในป๊อปอัปได้โดยการตั้งค่าตัวเลือก header และ content ดังนี้

async function init() {
  const {Map3DElement, MapMode, PopoverElement, AltitudeMode} = await google.maps.importLibrary('maps3d');

  const map = new Map3DElement({
    center: { lat: 37.819852, lng: -122.478549, altitude: 2000 },
    tilt: 75,
    heading: 330,
    mode: MapMode.SATELLITE,
  });

  const popover = new PopoverElement({
    altitudeMode: AltitudeMode.ABSOLUTE,
    open: true,
    positionAnchor: {lat: 37.819852, lng: -122.478549, altitude: 100},
  });

  const header = document.createElement('div');
  header.style.fontWeight = 'bold';
  header.slot = 'header';
  header.textContent = 'Golden Gate Bridge';
  const content = document.createElement('div');
  content.textContent = 'Iconic orange bridge connecting San Francisco to Marin.';

  popover.append(header);
  popover.append(content);

  document.body.append(map);
  map.append(popover);
}

init();