您可以在 3D 地圖中加入攝影機路徑動畫,為使用者提供更身歷其境的體驗。攝影機路徑動畫可以飛往或環繞地圖上的某個點。
以下範例結合了 flyCameraTo 和 flyCameraAround 動畫:
查看完整範例原始碼
TypeScript
async function init() { const { Map3DElement } = await google.maps.importLibrary('maps3d'); const map = new Map3DElement({ center: { lat: 37.79810773998413, lng: -122.41784275049939, altitude: 89.08476932205978, }, range: 6062.016931506805, tilt: 81.17100663963272, heading: -56.047035719765596, gestureHandling: 'COOPERATIVE', }); map.mode = 'SATELLITE'; document.body.append(map); // Used for both the fly to function and the location to fly around. const flyToCamera = { center: { lat: 21.263523536467105, lng: -157.80663691939296, altitude: 80.28936069489404, }, range: 1500.8202963253427, tilt: 76.9173260789542, heading: -44.59196007522445, }; // Fly the camera from San Francisco to Hawaii, can be controlled by a button alternatively. map.flyCameraTo({ // Where we are going to. endCamera: flyToCamera, // How long we want the flight to take. durationMillis: 30000, }); // When the animation has completed, fly around the location. map.addEventListener( 'gmp-animationend', () => { map.flyCameraAround({ // Location to fly around. camera: flyToCamera, // Length of time to fly to the location. durationMillis: 50000, // Number of rotations to make in the specified time. repeatCount: 1, }); }, { once: true } ); // Stop animation after flying around. // At any time stop the animation. map.addEventListener('gmp-click', () => { map.stopCameraAnimation(); }); } void init();
JavaScript
async function init() { const { Map3DElement } = await google.maps.importLibrary('maps3d'); const map = new Map3DElement({ center: { lat: 37.79810773998413, lng: -122.41784275049939, altitude: 89.08476932205978, }, range: 6062.016931506805, tilt: 81.17100663963272, heading: -56.047035719765596, gestureHandling: 'COOPERATIVE', }); map.mode = 'SATELLITE'; document.body.append(map); // Used for both the fly to function and the location to fly around. const flyToCamera = { center: { lat: 21.263523536467105, lng: -157.80663691939296, altitude: 80.28936069489404, }, range: 1500.8202963253427, tilt: 76.9173260789542, heading: -44.59196007522445, }; // Fly the camera from San Francisco to Hawaii, can be controlled by a button alternatively. map.flyCameraTo({ // Where we are going to. endCamera: flyToCamera, // How long we want the flight to take. durationMillis: 30000, }); // When the animation has completed, fly around the location. map.addEventListener( 'gmp-animationend', () => { map.flyCameraAround({ // Location to fly around. camera: flyToCamera, // Length of time to fly to the location. durationMillis: 50000, // Number of rotations to make in the specified time. repeatCount: 1, }); }, { once: true } ); // Stop animation after flying around. // At any time stop the animation. map.addEventListener('gmp-click', () => { map.stopCameraAnimation(); }); } void init();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ html, map { height: 100%; } 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: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8"
});
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>試用範例
預設攝影機路徑
Maps JavaScript 中的 3D 地圖提供兩種預設攝影機路徑。您可以變更動畫的持續時間 (藉此加快或減慢速度),或是結合多個攝影機路徑,打造更具電影感的體驗,自訂攝影機路徑。此外,您也可以指定 altitudeMode 來控制攝影機的高度。
Maps JavaScript 中的 3D 地圖支援下列攝影機路徑:
flyCameraTo動畫會從地圖中心飛往指定目的地。flyCameraAround動畫會繞著地圖上的某個點旋轉,旋轉次數為指定次數。
你可以結合這兩種路徑,先飛往搜尋點,再繞著該點旋轉,然後在指定位置停止。
目的地
下列程式碼範例說明如何將攝影機動畫設為飛往某個位置:
map.flyCameraTo({ // Where we are going to. endCamera: flyToCamera, // How long we want the flight to take. durationMillis: 30000, });
飛越
以下程式碼範例說明如何將攝影機動畫設為在某個位置附近飛行:
map.flyCameraAround({ // Location to fly around. camera: flyToCamera, // Length of time to fly to the location. durationMillis: 50000, // Number of rotations to make in the specified time. repeatCount: 1, });
合併動畫
下列程式碼範例會示範如何合併動畫,將攝影機飛往某個位置,然後在第一個動畫結束時繞著該位置旋轉:
async function init() { const { Map3DElement } = await google.maps.importLibrary('maps3d'); const map = new Map3DElement({ center: { lat: 37.79810773998413, lng: -122.41784275049939, altitude: 89.08476932205978, }, range: 6062.016931506805, tilt: 81.17100663963272, heading: -56.047035719765596, gestureHandling: 'COOPERATIVE', }); map.mode = 'SATELLITE'; document.body.append(map); // Used for both the fly to function and the location to fly around. const flyToCamera = { center: { lat: 21.263523536467105, lng: -157.80663691939296, altitude: 80.28936069489404, }, range: 1500.8202963253427, tilt: 76.9173260789542, heading: -44.59196007522445, }; // Fly the camera from San Francisco to Hawaii, can be controlled by a button alternatively. map.flyCameraTo({ // Where we are going to. endCamera: flyToCamera, // How long we want the flight to take. durationMillis: 30000, }); // When the animation has completed, fly around the location. map.addEventListener( 'gmp-animationend', () => { map.flyCameraAround({ // Location to fly around. camera: flyToCamera, // Length of time to fly to the location. durationMillis: 50000, // Number of rotations to make in the specified time. repeatCount: 1, }); }, { once: true } ); // Stop animation after flying around. // At any time stop the animation. map.addEventListener('gmp-click', () => { map.stopCameraAnimation(); }); } void init();