In this javascript I want to add a map.fitBounds(bounds); statement to the else…
- User
In this javascript I want to add a map.fitBounds(bounds); statement to the else statement after 'const finalScores =
Total Score: ${totalScore};' so that the map is fitted to the bounds of all the "Points" locations in 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 variablecodefunction 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" } ] - Assistant
else { const finalScores =
Total Score: ${totalScore}; map.fitBounds(bounds); // Fit map to boundscode// Update the 'streetview' div with final scores const streetviewDiv = document.getElementById("streetview"); streetviewDiv.innerHTML = finalScores; // Update content with innerHTML // Update the 'results' div with final scores const resultsDiv = document.getElementById("results"); resultsDiv.textContent = overmessage;}
- User
where have you defined the bounds to fit all the 'Points' from the json file
- Assistant
To define the bounds to fit all the "Points" locations from the JSON file, you can iterate over each "Point" in the JSON data and extend the bounds to include each point's coordinates. Here is how you can modify the code to calculate the bounds based on the "Points" locations:
javascriptelse { const finalScores = `Total Score: ${totalScore}`; // Create LatLngBounds object to store the bounds const bounds = new google.maps.LatLngBounds(); // Iterate over each point in the data and extend the bounds data[currentIndex].Points.forEach((point) => { bounds.extend(new google.maps.LatLng(point.lat, point.lng)); }); map.fitBounds(bounds); // Fit map to bounds // Update the 'streetview' div with final scores const streetviewDiv = document.getElementById("streetview"); streetviewDiv.innerHTML = finalScores; // Update content with innerHTML // Update the 'results' div with final scores const resultsDiv = document.getElementById("results"); resultsDiv.textContent = overmessage; }