In the else statement in this javascript I want to fit the mapbounds to the extent of…
- User
In the else statement in this javascript I want to fit the mapbounds to the extent of all the streetDetails.Points fetched from the json file 'let streetLatitude; // Define streetLatitude globally let streetLongitude; // Define streetLongitude globally let marker; // Define marker globally to make it accessible across functions let data; // Declare data globally let currentIndex = 0; // Initialize index globally let totalScore = 0; // Initialize total points variable
function fetchStreetDetails() { fetch("main.json") .then((response) => response.json()) .then((jsonData) => { data = jsonData; // Store the data globally const entryCount = data.length; // data is already an array of objects const streetDetails = data[0]; // Extract street details streetLatitude = streetDetails.StreetLatitude; streetLongitude = streetDetails.StreetLongitude; streetHeading = streetDetails.StreetHeading; streetPitch = streetDetails.StreetPitch; streetPanoID = streetDetails.StreetPanoID; const StreetPoints = streetDetails.Points; const panorama = new google.maps.StreetViewPanorama( document.getElementById("streetview"), { position: { lat: streetLatitude, lng: streetLongitude }, addressControl: false, pano: streetPanoID, heading: streetHeading, pitch: streetPitch, } ); console.log("Street Latitude: " + streetLatitude); console.log("Street Longitude: " + streetLongitude); console.log("Street Heading: " + streetHeading); console.log("Street Pitch: " + streetPitch); console.log("Street PanoID: " + streetPanoID); console.log("Street Location: " + StreetPoints); // Update numberoffeeds div const numberoffeedsElement = document.getElementById("numberoffeeds"); numberoffeedsElement.textContent = `There are ${entryCount} questions in this game.`; }) .catch((error) => console.error("Error fetching data: ", error)); } fetchStreetDetails(); function initMap() { const mapStyles = [ { featureType: "poi", stylers: [ { visibility: "off", }, ], }, { featureType: "poi.park", stylers: [ { visibility: "off", }, ], }, { "featureType": "transit", "stylers": [ { "visibility": "off" } ]} ];
const mapOptions = { center: { lat: 21.382325, lng: -8.170154652 }, zoom: 3, styles: mapStyles, }; const map = new google.maps.Map( document.getElementById("map"), mapOptions ); // Function to add marker on click function addMarker(event) { const clickLocation = event.latLng; marker = new google.maps.Marker({ position: clickLocation, map: map, draggable: true, // Set draggable to true }); // Remove the click listener after adding a marker google.maps.event.removeListener(clickListener); // Add functionality after clicking the map createSubmitButton(clickLocation); } // Define the CSS animation style separately (outside the button creation) - Assistant
To fit the map bounds to the extent of all the streetDetails.Points fetched from the json file in the else statement, you can make use of the Array.reduce() method to calculate the bounds of all the points and then fit the map to those bounds. Here's how you can modify the else statement:
} else { console.log("No next entry in the data."); // Calculate total points const finalScores = `Total Score: ${totalScore}`; // Update the 'streetview' div with final scores const streetviewDiv = document.getElementById("streetview"); streetviewDiv.innerHTML = finalScores; // Update content with innerHTML const overmessage = "Game Over"; const resultsDiv = document.getElementById("results"); resultsDiv.textContent = overmessage; // Calculate the bounds of all street points const allPoints = [].concat.apply([], data.map(item => item.Points)); const bounds = allPoints.reduce((bounds, point) => { return bounds.extend(new google.maps.LatLng(point.lat, point.lng)); }, new google.maps.LatLngBounds()); // Fit the map to the bounds of all street points map.fitBounds(bounds); } - User
in this javascript I want to remove all text from the 'question' div element - const newmessage = " "; const newDiv = document.getElementById("question"); resultsDiv.textContent = newmessage;
- Assistant
To remove all text from the 'question' div element, you can set its
textContentto an empty string. Here's how you can update the code snippet:const newmessage = ""; // Set an empty string to remove all text const questionDiv = document.getElementById("question"); questionDiv.textContent = newmessage; // Set the text content of the question div to an empty stringWith this code, the text content of the 'question' div element will be cleared as it will be set to an empty string.