Models are 3D assets that can be added to scene to represent objects. The Photorealistic 3D Maps in Maps JavaScript API supports the inclusion of gLTF models. These models can be scaled and rotated to allow for further customization.
The models must support the core properties of the glTF PBR. No extensions or extension properties are supported.
Add a model
The
Model3DElement
constructor takes a set of Model3DElementOptions
specifying the LatLng
coordinates of the model and a set of parameters to support positioning the
model.
Models support the following options:
position
: The location of the model expressed inLatLng
coordinates.orientation
: The rotation of the model's coordinate system. Rotations are applied in the following order: roll, tilt and then heading.scale
: Scales the model in its coordinate space. The default value is1
.altitudeMode
: How the altitude expressed inposition
is interpreted.src
: The URL of the model.
The following example demonstrates adding models to the map and modifying them using the available customization options:
async function init() {
const { Map3DElement, MapMode, Model3DElement } = await google.maps.importLibrary("maps3d");
const map = new Map3DElement({
center: {lat: 37.7438, lng: -121.5088, altitude: 1800},
heading: -90,
tilt: 90,
mode: MapMode.SATELLITE,
});
document.body.append(map);
const models = [
// A model with regular settings.
{
position: {lat: 37.76, lng: -121.63, altitude: 0},
orientation: {tilt: 270},
},
// Scaled down model.
{
position: {lat: 37.75, lng: -121.63, altitude: 0},
orientation: {tilt: 270},
scale: 0.8,
},
// Scaled up model.
{
position: {lat: 37.735, lng: -121.63, altitude: 0},
orientation: {tilt: 270},
scale: 1.2,
},
// A model with an additional transformation applied.
{
position: {lat: 37.72, lng: -121.63, altitude: 0},
orientation: {tilt: 270, roll: 90},
},
// Non-clamped to the ground model.
{
position: {lat: 37.71, lng: -121.63, altitude: 1000},
altitudeMode: 'RELATIVE_TO_GROUND',
orientation: {tilt: 270},
},
];
for (const {position, altitudeMode, orientation, scale} of models) {
const model = new Model3DElement({
src: 'https://storage.googleapis.com/maps-web-api.appspot.com/gltf/windmill.glb',
position,
altitudeMode,
orientation,
scale,
});
map.append(model);
}
}
init();
Add an interactive model
Models also support interaction. The following example changes the model's scale when clicked.
async function init() {
const { Map3DElement, MapMode, Model3DInteractiveElement } = await google.maps.importLibrary("maps3d");
const map = new Map3DElement({
center: {lat: 37.7438, lng: -121.5088, altitude: 1800},
heading: -90,
tilt: 90,
mode: MapMode.SATELLITE,
});
document.body.append(map);
const model = new Model3DInteractiveElement({
src: 'https://storage.googleapis.com/maps-web-api.appspot.com/gltf/windmill.glb',
position: {lat: 37.76, lng: -121.63, altitude: 0},
scale: 1.0,
});
model.addEventListener('gmp-click', (event) => {
model.scale = (Math.random() * (1 - 2)).toFixed(2);
});
map.append(model);
}
init();