A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/maps/documentation/javascript/webgl/webgl-overlay-view below:

WebGL Overlay View | Maps JavaScript API

WebGL Overlay View

Stay organized with collections Save and categorize content based on your preferences.

View Sample

With WebGL Overlay View you can add content to your maps using WebGL directly, or popular Graphics libraries like Three.js. WebGL Overlay View provides direct access to the same WebGL rendering context Google Maps Platform uses to render the vector basemap. This use of a shared rendering context provides benefits such as depth occlusion with 3D building geometry, and the ability to sync 2D/3D content with basemap rendering. Objects rendered with the WebGL Overlay View can also be tied to latitude/longitude coordinates, so they move when you drag, zoom, pan, or tilt the map.

Requirements

To use WebGL Overlay View, you must load the map using a map ID with the vector map enabled. We strongly recommend enabling tilt and rotation when you create the map ID, to allow for full 3D camera control. See the overview for details.

Add WebGL Overlay View

To add the overlay to your map, implement google.maps.WebGLOverlayView, then pass it your map instance using setMap:

// Create a map instance.
const map = new google.maps.Map(mapDiv, mapOptions);

// Create a WebGL Overlay View instance.
const webglOverlayView = new google.maps.WebGLOverlayView();

// Add the overlay to the map.
webglOverlayView.setMap(map);
Lifecycle hooks

WebGL Overlay View provides a set of hooks that are called at various times in the lifecycle of the WebGL rendering context of the vector basemap. These lifecycle hooks are where you setup, draw, and tear down anything you want rendered in the overlay.

For example, the following is a basic implementation of all lifecycle hooks:

const webglOverlayView = new google.maps.WebGLOverlayView();

webglOverlayView.onAdd = () => {
  // Do setup that does not require access to rendering context.
}

webglOverlayView.onContextRestored = ({gl}) => {
  // Do setup that requires access to rendering context before onDraw call.
}

webglOverlayView.onStateUpdate = ({gl}) => {
  // Do GL state setup or updates outside of the render loop.
}

webglOverlayView.onDraw = ({gl, transformer}) => {
  // Render objects.
}

webglOverlayView.onContextLost = () => {
  // Clean up pre-existing GL state.
}

webglOverlayView.onRemove = () => {
  // Remove all intermediate objects.
}

webglOverlayView.setMap(map);
Resetting GL State

WebGL Overlay View exposes the WebGL rendering context of the basemap. Because of this, it is extremely important that you reset the GL state to its original state when you are done rendering objects. Failure to reset the GL state is likely to result in GL state conflicts, which will cause rendering of both the map and any objects you specify to fail.

Resetting the GL state is normally handled in the onDraw() hook. For example, Three.js provides a helper function that clears any changes to the GL state:

webglOverlayView.onDraw = ({gl, transformer}) => {
  // Specify an object to render.
  renderer.render(scene, camera);
  renderer.resetState();
}

If the map or your objects fail to render, it is very likely that the GL state has not been reset.

Coordinate Transformations

The position of an object on the vector map is specified by providing a combination of latitude and longitude coordinates, as well as altitude. 3D graphics, however, are specified in world space, camera space, or screen space. To make it easier to transform map coordinates to these more commonly used spaces, WebGL Overlay View provides the coordinateTransformer.fromLatLngAltitude(latLngAltitude, rotationArr, scalarArr) helper function in the onDraw() hook that takes the following and returns a Float64Array:

For example, the following uses fromLatLngAltitude() to create a camera projection matrix in Three.js:

const camera = new THREE.PerspectiveCamera();
const matrix = coordinateTransformer.fromLatLngAltitude({
    lat: mapOptions.center.lat,
    lng: mapOptions.center.lng,
    altitude: 120,
});
camera.projectionMatrix = new THREE.Matrix4().fromArray(matrix);
Example

The following is a simple example of using Three.js, a popular, open source WebGL library, to place a 3D object on the map. For a complete walkthrough of using WebGL Overlay View to build the example you see running at the top of this page, try the Building WebGL-accelerated Map Experiences codelab.

const webglOverlayView = new google.maps.WebGLOverlayView();
let scene, renderer, camera, loader;

webglOverlayView.onAdd = () => {
  // Set up the Three.js scene.
  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera();
  const ambientLight = new THREE.AmbientLight( 0xffffff, 0.75 ); // Soft white light.
  scene.add(ambientLight);

  // Load the 3D model with GLTF Loader from Three.js.
  loader = new GLTFLoader();
  loader.load("pin.gltf");
}

webglOverlayView.onContextRestored = ({gl}) => {
  // Create the Three.js renderer, using the
  // maps's WebGL rendering context.
  renderer = new THREE.WebGLRenderer({
    canvas: gl.canvas,
    context: gl,
    ...gl.getContextAttributes(),
  });
  renderer.autoClear = false;
}

webglOverlayView.onDraw = ({gl, transformer}) => {
  // Update camera matrix to ensure the model is georeferenced correctly on the map.
  const matrix = transformer.fromLatLngAltitude({
      lat: mapOptions.center.lat,
      lng: mapOptions.center.lng,
      altitude: 120,
  });
camera.projectionMatrix = new THREE.Matrix4().fromArray(matrix);

  // Request a redraw and render the scene.
  webglOverlayView.requestRedraw();
  renderer.render(scene, camera);

  // Always reset the GL state.
  renderer.resetState();
}

// Add the overlay to the map.
webglOverlayView.setMap(map);

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

Last updated 2025-07-09 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-09 UTC."],[[["WebGL Overlay View empowers you to seamlessly integrate 3D and 2D content into Google Maps using WebGL directly or via libraries like Three.js, leveraging the same rendering context for synchronized visuals and depth occlusion with buildings."],["It's crucial to load the map with a vector map enabled map ID, ideally with tilt and rotation activated, to fully utilize 3D camera control and functionalities."],["WebGL Overlay View offers lifecycle hooks like `onAdd`, `onContextRestored`, `onDraw`, `onContextLost`, `onStateUpdate`, and `onRemove`, providing control over setup, rendering, and teardown of your overlay elements."],["Remember to reset the GL state after rendering using methods like `renderer.resetState()` (Three.js) to avoid rendering conflicts and ensure proper functionality."],["Utilize the `coordinateTransformer.fromLatLngAltitude()` helper function within `onDraw` to transform latitude/longitude/altitude coordinates to world/camera/screen space for accurate object placement and georeferencing."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4