限制摄像头画面和边界

请选择平台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”手势处理允许用户滚动页面,而不会影响地图的缩放或平移。如需缩放,用户可以使用控件、双指手势(适用于触摸屏设备),或者在滚动时按住 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”。