カメラビューと境界を制限する

プラットフォームを選択: 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

「協調」ジェスチャー処理では、地図のズームやパンに影響を与えることなく、ページをスクロールできます。ズームするには、コントロール、2 本指のジェスチャー(タッチスクリーン デバイスの場合)、または CMD/CTRL を押しながらスクロールします。

次のコードは、ジェスチャー処理を「協調的」に設定する方法を示しています。

<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

「自動」ジェスチャー処理では、地図が <iframe> に含まれているかどうか、ページがスクロール可能かどうかによって、地図の動作が変わります。

  • マップが <iframe> 内にある場合、ジェスチャー処理は「協調的」になります。
  • 地図が <iframe> 内にない場合、ジェスチャー操作は「greedy」になります。