This is how you can control the camera in a Re-Earth plugin using an HTML interface. Users can input values for various camera properties like latitude, longitude, height, heading, pitch, roll, range, and duration. Then, they can choose to either "Fly to" or "Look at" a specific location with the provided camera settings.
const html = `
<style>
html, body {
margin: 0;
width: 350px;
background: white;
}
#wrapper {
height: 100%;
padding: 12px;
box-sizing: border-box;
}
input {
width: 60px;
}
</style>
<div id="wrapper">
<p>Longitude: <input type="number" id="lng" value="139.78" />°</p>
<p>Latitude: <input type="number" id="lat" value="35.68" />°</p>
<p>Height: <input type="number" id="height" value="10000" /> m</p>
<p>Heading: <input type="number" id="heading" value="0" step="0.1" /></p>
<p>Pitch: <input type="number" id="pitch" value="-1.5" step="0.1" /></p>
<p>Roll (for fly to): <input type="number" id="roll" value="0" step="0.1" /></p>
<p>Range (for look at): <input type="number" id="range" value="10000" /> m</p>
<p>Duration: <input type="number" id="duration" value="2" /> s</p>
<p>
<button id="fly">Fly to</button>
<button id="look">Look at</button>
</p>
</div>
<script>
// Function to get input values
const getValues = () => ({
lng: parseFloat(document.getElementById("lng").value || 0),
lat: parseFloat(document.getElementById("lat").value || 0),
height: parseFloat(document.getElementById("height").value || 0),
heading: parseFloat(document.getElementById("heading").value || 0),
pitch: parseFloat(document.getElementById("pitch").value || 0),
roll: parseFloat(document.getElementById("roll").value || 0),
range: parseFloat(document.getElementById("range").value || 0),
duration: parseFloat(document.getElementById("duration").value || 0),
});
// Event listener for "Fly to" button
document.getElementById("fly").addEventListener("click", () => {
parent.postMessage({ fly: getValues() }, "*");
});
// Event listener for "Look at" button
document.getElementById("look").addEventListener("click", () => {
parent.postMessage({ look: getValues() }, "*");
});
</script>
`;
// Display the UI
reearth.ui.show(html);
// Listen for messages from Re:Earth
reearth.on("message", msg => {
if (msg.fly) {
// Perform camera flyTo
reearth.camera.flyTo(
{
lat: msg.fly.lat,
lng: msg.fly.lng,
height: msg.fly.height,
heading: msg.fly.heading,
pitch: msg.fly.pitch,
roll: msg.fly.roll,
},
{
duration: msg.fly.duration,
}
);
} else if (msg.look) {
// Perform camera lookAt
reearth.camera.lookAt(
{
lat: msg.look.lat,
lng: msg.look.lng,
height: msg.look.height,
heading: msg.look.heading,
pitch: msg.look.pitch,
range: msg.look.range,
},
{
duration: msg.look.duration,
}
);
}
});
html
variable contains an HTML template for the UI. It sets up a styled form with input fields for longitude, latitude, height, heading, pitch, roll, range, and duration. There are also two buttons labeled "Fly to" and "Look at".<script>
tag, the getValues
function is defined. This function reads the values entered in the input fields and returns them as an object.getValues
function.reearth.ui.show(html)
line displays the UI created by the html
template.reearth.on("message", msg => {...})
. When a message is received, the code checks if it contains either "fly" or "look" parameters.Note that: In summary, this code creates a UI that allows users to set camera parameters and trigger camera movement actions in Re-Earth. It demonstrates how to interact with Re-Earth's camera functionality using a simple interface.