จำกัดมุมมองกล้องและขอบเขต

เลือกแพลตฟอร์ม: Android iOS JavaScript

คุณอาจต้องการควบคุมการแพนกล้อง ระดับความสูงสูงสุด หรือสร้างขอบเขตละติจูดและลองจิจูดเพื่อจำกัดการเคลื่อนไหวของผู้ใช้ในแผนที่ที่กำหนด ซึ่งทำได้โดยใช้ ข้อจำกัดของกล้อง

ตัวอย่างต่อไปนี้แสดงแผนที่ที่มีการตั้งค่าขอบเขตสถานที่ตั้งเพื่อจำกัดการเคลื่อนไหวของกล้อง

จำกัดขอบเขตของแผนที่

คุณสามารถจำกัดขอบเขตทางภูมิศาสตร์ของกล้องได้โดยตั้งค่าตัวเลือก bounds

ตัวอย่างโค้ดต่อไปนี้แสดงการจำกัดขอบเขตของแผนที่

TypeScript

async function init(): Promise<void> {
    await google.maps.importLibrary('maps3d');

    const map3DElement = document.querySelector('gmp-map-3d')!;

    // Restrict the position of the camera to the specified bounds.
    map3DElement.bounds = {
        south: -48.3,
        west: 163.56,
        north: -32.86,
        east: -180,
    };
}

void init();

JavaScript

async function init() {
    await google.maps.importLibrary('maps3d');

    const map3DElement = document.querySelector('gmp-map-3d');

    // Restrict the position of the camera to the specified bounds.
    map3DElement.bounds = {
        south: -48.3,
        west: 163.56,
        north: -32.86,
        east: -180,
    };
}

void init();

CSS

html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

HTML

<html>
    <head>
        <title>Map</title>

        <link rel="stylesheet" type="text/css" href="./style.css" />
        <script type="module" src="./index.js"></script>
        <script>
            // prettier-ignore
            (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
                key: "GOOGLE_MAPS_API_KEY"
            });
        </script>
    </head>
    <body>
        <gmp-map-3d
            id="gmp-map-3d"
            center="-47.1375,169.5235,150000"
            tilt="67.5"
            mode="hybrid"
            gesture-handling="cooperative">
        </gmp-map-3d>
    </body>
</html>

จำกัดกล้อง

คุณสามารถจำกัดการเคลื่อนไหวของกล้องได้โดยตั้งค่าตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้

  • maxAltitude
  • minAltitude
  • maxHeading
  • minHeading
  • maxTilt
  • minTilt

ตัวอย่างโค้ดต่อไปนี้แสดงการจำกัดกล้อง

// Restrict the movement of the camera.
map3DElement.maxAltitude = 1000;
map3DElement.minAltitude = 500;
map3DElement.maxTilt = 55;
map3DElement.minTilt = 35;

จำกัดขอบเขตของแผนที่และกล้อง

คุณสามารถจำกัดขอบเขตของแผนที่และกล้องได้พร้อมกัน ตัวอย่างโค้ดต่อไปนี้แสดงการจำกัดขอบเขตของแผนที่และกล้อง

// Restrict the movement of the camera.
map3DElement.maxAltitude = 1000;
map3DElement.minAltitude = 500;
map3DElement.maxTilt = 55;
map3DElement.minTilt = 35;

// Restrict the position of the camera to the specified bounds.
map3DElement.bounds = {
    north: 20.93,
    west: -156.38,
    south: 20.90,
    east: -156.31,
};

ควบคุมการจัดการท่าทางสัมผัส

เมื่อผู้ใช้เลื่อนหน้าเว็บที่มีแผนที่ การเลื่อนอาจทำให้แผนที่ซูมโดยไม่ตั้งใจ คุณสามารถควบคุมลักษณะการทำงานนี้ได้โดยตั้งค่าตัวเลือกแผนที่ gestureHandling

gestureHandling: cooperative

การจัดการท่าทางสัมผัสแบบ "Cooperative" ช่วยให้ผู้ใช้เลื่อนหน้าเว็บได้โดยไม่ส่งผลต่อการซูมหรือแพนแผนที่ หากต้องการซูม ผู้ใช้สามารถใช้การควบคุม ท่าทางสัมผัสด้วย 2 นิ้ว (สำหรับอุปกรณ์หน้าจอสัมผัส) หรือกด CMD/CTRL ค้างไว้ขณะเลื่อน

โค้ดต่อไปนี้แสดงการตั้งค่าการจัดการท่าทางสัมผัสเป็น "cooperative"

<gmp-map-3d
    id="gmp-map-3d"
    center="20.92,-156.36,500"
    tilt="67.5"
    mode="hybrid"
    gesture-handling="cooperative">
</gmp-map-3d>

gestureHandling: greedy

การจัดการท่าทางสัมผัสแบบ "Greedy" จะตอบสนองต่อเหตุการณ์การเลื่อนและท่าทางสัมผัสทั้งหมด

gestureHandling: auto

การจัดการท่าทางสัมผัสแบบ "Auto" จะเปลี่ยนลักษณะการทำงานของแผนที่โดยขึ้นอยู่กับว่าแผนที่อยู่ใน หรือไม่ และหน้าเว็บเลื่อนได้หรือไม่<iframe>

  • หากแผนที่อยู่ใน <iframe> การจัดการท่าทางสัมผัสจะเป็น "cooperative"
  • หากแผนที่ไม่อยู่ใน <iframe> การจัดการท่าทางสัมผัสจะเป็นแบบ "greedy"