Places UI Kit: A ready-to-use library that provides room for customization and low-code development. Try it out, and share yourinput on your UI Kit experience.

WebGL Overlay (native API)

  • This example showcases the use ofWebGLOverlayView to render a 3D model with animation on a Google Map.

  • It uses the Three.js library for 3D rendering and a GLTF model of a pin as the displayed object.

  • TheWebGLOverlayView handles the integration between the 3D scene and the map, ensuring proper georeferencing and rendering.

  • The code includes a simple animation loop that rotates the map and the 3D model, providing a dynamic visual experience.

  • Users can interact with the provided code snippets in Typescript, JavaScript, CSS, and HTML, and there's an option to try a live sample or clone the repository for local execution.

This example demonstrates using theWebGLOverlayView to display a 3D imagewith animation on a vector map.

Read thedocumentation.

TypeScript

import{AmbientLight,DirectionalLight,Matrix4,PerspectiveCamera,Scene,WebGLRenderer,}from"three";import{GLTFLoader}from"three/examples/jsm/loaders/GLTFLoader";letmap:google.maps.Map;constmapOptions={tilt:0,heading:0,zoom:18,center:{lat:35.6594945,lng:139.6999859},mapId:"15431d2b469f209e",// disable interactions due to animation loop and moveCameradisableDefaultUI:true,gestureHandling:"none",keyboardShortcuts:false,};functioninitMap():void{constmapDiv=document.getElementById("map")asHTMLElement;map=newgoogle.maps.Map(mapDiv,mapOptions);initWebglOverlayView(map);}functioninitWebglOverlayView(map:google.maps.Map):void{letscene,renderer,camera,loader;constwebglOverlayView=newgoogle.maps.WebGLOverlayView();webglOverlayView.onAdd=()=>{// Set up the scene.scene=newScene();camera=newPerspectiveCamera();constambientLight=newAmbientLight(0xffffff,0.75);// Soft white light.scene.add(ambientLight);constdirectionalLight=newDirectionalLight(0xffffff,0.25);directionalLight.position.set(0.5,-1,0.5);scene.add(directionalLight);// Load the model.loader=newGLTFLoader();constsource="https://raw.githubusercontent.com/googlemaps/js-samples/main/assets/pin.gltf";loader.load(source,(gltf)=>{gltf.scene.scale.set(10,10,10);gltf.scene.rotation.x=Math.PI;// Rotations are in radians.scene.add(gltf.scene);});};webglOverlayView.onContextRestored=({gl})=>{// Create the js renderer, using the// maps's WebGL rendering context.renderer=newWebGLRenderer({canvas:gl.canvas,context:gl,...gl.getContextAttributes(),});renderer.autoClear=false;// Wait to move the camera until the 3D model loads.loader.manager.onLoad=()=>{renderer.setAnimationLoop(()=>{webglOverlayView.requestRedraw();const{tilt,heading,zoom}=mapOptions;map.moveCamera({tilt,heading,zoom});// Rotate the map 360 degrees.if(mapOptions.tilt <67.5){mapOptions.tilt+=0.5;}elseif(mapOptions.heading<=360){mapOptions.heading+=0.2;mapOptions.zoom-=0.0005;}else{renderer.setAnimationLoop(null);}});};};webglOverlayView.onDraw=({gl,transformer}):void=>{constlatLngAltitudeLiteral:google.maps.LatLngAltitudeLiteral={lat:mapOptions.center.lat,lng:mapOptions.center.lng,altitude:100,};// Update camera matrix to ensure the model is georeferenced correctly on the map.constmatrix=transformer.fromLatLngAltitude(latLngAltitudeLiteral);camera.projectionMatrix=newMatrix4().fromArray(matrix);webglOverlayView.requestRedraw();renderer.render(scene,camera);// Sometimes it is necessary to reset the GL state.renderer.resetState();};webglOverlayView.setMap(map);}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
Note: Read theguide on using TypeScript and Google Maps.

JavaScript

import{AmbientLight,DirectionalLight,Matrix4,PerspectiveCamera,Scene,WebGLRenderer,}from"three";import{GLTFLoader}from"three/examples/jsm/loaders/GLTFLoader";letmap;constmapOptions={tilt:0,heading:0,zoom:18,center:{lat:35.6594945,lng:139.6999859},mapId:"15431d2b469f209e",// disable interactions due to animation loop and moveCameradisableDefaultUI:true,gestureHandling:"none",keyboardShortcuts:false,};functioninitMap(){constmapDiv=document.getElementById("map");map=newgoogle.maps.Map(mapDiv,mapOptions);initWebglOverlayView(map);}functioninitWebglOverlayView(map){letscene,renderer,camera,loader;constwebglOverlayView=newgoogle.maps.WebGLOverlayView();webglOverlayView.onAdd=()=>{// Set up the scene.scene=newScene();camera=newPerspectiveCamera();constambientLight=newAmbientLight(0xffffff,0.75);// Soft white light.scene.add(ambientLight);constdirectionalLight=newDirectionalLight(0xffffff,0.25);directionalLight.position.set(0.5,-1,0.5);scene.add(directionalLight);// Load the model.loader=newGLTFLoader();constsource="https://raw.githubusercontent.com/googlemaps/js-samples/main/assets/pin.gltf";loader.load(source,(gltf)=>{gltf.scene.scale.set(10,10,10);gltf.scene.rotation.x=Math.PI;// Rotations are in radians.scene.add(gltf.scene);});};webglOverlayView.onContextRestored=({gl})=>{// Create the js renderer, using the// maps's WebGL rendering context.renderer=newWebGLRenderer({canvas:gl.canvas,context:gl,...gl.getContextAttributes(),});renderer.autoClear=false;// Wait to move the camera until the 3D model loads.loader.manager.onLoad=()=>{renderer.setAnimationLoop(()=>{webglOverlayView.requestRedraw();const{tilt,heading,zoom}=mapOptions;map.moveCamera({tilt,heading,zoom});// Rotate the map 360 degrees.if(mapOptions.tilt <67.5){mapOptions.tilt+=0.5;}elseif(mapOptions.heading<=360){mapOptions.heading+=0.2;mapOptions.zoom-=0.0005;}else{renderer.setAnimationLoop(null);}});};};webglOverlayView.onDraw=({gl,transformer})=>{constlatLngAltitudeLiteral={lat:mapOptions.center.lat,lng:mapOptions.center.lng,altitude:100,};// Update camera matrix to ensure the model is georeferenced correctly on the map.constmatrix=transformer.fromLatLngAltitude(latLngAltitudeLiteral);camera.projectionMatrix=newMatrix4().fromArray(matrix);webglOverlayView.requestRedraw();renderer.render(scene,camera);// Sometimes it is necessary to reset the GL state.renderer.resetState();};webglOverlayView.setMap(map);}window.initMap=initMap;
Note: The JavaScript is compiled from the TypeScript snippet.

CSS

/* * Always set the map height explicitly to define the size of the div element * that contains the map. */#map{height:100%;}/* * Optional: Makes the sample page fill the window. */html,body{height:100%;margin:0;padding:0;}#map{pointer-events:none;}

HTML

<html>  <head>    <title>Simple WebGL Overlay</title>    <script src="https://unpkg.com/three@0.129.0/build/three.min.js"></script>    <link rel="stylesheet" type="text/css" href="./style.css" />    <script type="module" src="./index.js"></script>  </head>  <body>    <div></div>    <!--       The `defer` attribute causes the script to execute after the full HTML      document has been parsed. For non-blocking uses, avoiding race conditions,      and consistent behavior across browsers, consider loading using Promises. See      https://developers.google.com/maps/documentation/javascript/load-maps-js-api      for more information.      -->    <script      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly"      defer    ></script>  </body></html>

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow theseinstructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

gitclone-bsample-webgl-overlay-simplehttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart

Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.

gitcheckoutsample-SAMPLE_NAMEnpminpmstart

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-21 UTC.