โมเดลคือชิ้นงาน 3 มิติที่เพิ่มลงในฉากเพื่อแสดงออบเจ็กต์ได้ แผนที่ 3 มิติสมจริงใน Maps JavaScript API รองรับการรวมโมเดล gLTF โมเดลเหล่านี้สามารถ ปรับขนาดและหมุนเพื่อปรับแต่งเพิ่มเติมได้
โมเดลต้องรองรับพร็อพเพอร์ตี้หลักของ glTF PBR ไม่รองรับส่วนขยายหรือพร็อพเพอร์ตี้ส่วนขยาย
เพิ่มโมเดล
ตัวสร้างModel3DElement
จะใช้ชุดModel3DElementOptions
ที่ระบุLatLng
พิกัดของโมเดลและชุดพารามิเตอร์เพื่อรองรับการวางตำแหน่งโมเดล
โมเดลรองรับตัวเลือกต่อไปนี้
position
: ตำแหน่งของโมเดลที่แสดงในพิกัดLatLng
orientation
: การหมุนระบบพิกัดของโมเดล การหมุนจะ ใช้ตามลำดับต่อไปนี้ การหมุน การเอียง และการมุ่งหน้าscale
: ปรับขนาดโมเดลในพื้นที่พิกัด ค่าเริ่มต้นคือ1
altitudeMode
: วิธีตีความระดับความสูงที่แสดงในposition
src
: URL ของโมเดล
ตัวอย่างต่อไปนี้แสดงการเพิ่มโมเดลลงในแผนที่และการแก้ไขโมเดล โดยใช้ตัวเลือกการปรับแต่งที่มี
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();
เพิ่มโมเดลแบบอินเทอร์แอกทีฟ
นอกจากนี้ โมเดลยังรองรับการโต้ตอบด้วย ตัวอย่างต่อไปนี้จะเปลี่ยนสเกลของโมเดล เมื่อคลิก
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();