In this javascript why is the PaintingURL not being displayed 'let streetLatitude; let…
- User
In this javascript why is the PaintingURL not being displayed 'let streetLatitude; let streetLongitude; let marker; // Define marker globally to make it accessible across functions let totalScore = 0; // Initialize total points variable let possibleScore = 0; // Initialize total points variable let imageIndex = 0; // Initialize image index
function fetchStreetDetails(callback) { let imageIndex = 0; // Initialize imageIndex to 0 fetch('main.json') .then((response) => response.json()) .then((jsonData) => { const entryCount = jsonData.Features.length;
// Check if there are more images to display if (imageIndex >= entryCount) { console.log('No more images to display!'); return; } const streetDetails = jsonData.Features[imageIndex]; // Get image data based on index // Extract details const FeatureID = streetDetails.FeatureID; streetLatitude = streetDetails.StreetLatitude; streetLongitude = streetDetails.StreetLongitude; const streetHeading = streetDetails.StreetHeading; const streetPitch = streetDetails.StreetPitch; const streetPanoID = streetDetails.StreetPanoID; const StreetPoints = streetDetails.Points; const PictureURL = streetDetails.PictureURL; console.log('FeatureID: ' + FeatureID); console.log('PictureURL: ' + PictureURL); 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 // Update numberoffeeds divconst numberoffeedsElement = document.getElementById("results"); numberoffeedsElement.textContent =
This is a ${entryCount} round game.; callback(FeatureID); }) .catch((error) => console.error('Error fetching data: ', error)); }function initMap() { const mapOptions = { center: { lat: 21.382325, lng: -8.170154652 }, zoom: 3, };
const map = new google.maps.Map(document.getElementById("map"), mapOptions);
// Add a click event listener to the map const clickListener = map.addListener("click", (event) => { const clickLocation = event.latLng; // Get the latitude and longitude of the click
// Create a new marker marker = new google.maps.Marker({ position: clickLocation, map: map, // Set the map where the marker will be displayed draggable: true, // Set draggable to true }); // (Optional) Add additional customization to the marker here, // such as setting an icon or info window // Remove the click event listener after adding the marker google.maps.event.removeListener(clickListener); // Add functionality after clicking the map createSubmitButton(map, clickLocation);}); }
//nextbutton const nextButton = document.createElement("button"); nextButton.id = "nextButton"; nextButton.textContent = "Next"; // Customize button text as needed nextButton.className = "nextbutton";
- Assistant
The PaintingURL is not being displayed because it is not defined or assigned a value in the code. The variable
PaintingURLis not declared or initialized anywhere in the provided code snippet, so when you try to use it in theupdateImagefunction, it is undefined.To fix this issue, you need to ensure that the
PaintingURLvariable is properly defined and assigned a value before using it in theupdateImagefunction. You can retrieve thePictureURLfrom the JSON data and use it as thePaintingURLfor displaying the image.Here is an updated version of the
updateImagefunction with thePaintingURLvariable initialized with the value ofPictureURLfrom the JSON data:function updateImage(FeatureID) { const paintingDiv = document.getElementById("painting"); const imageHTML = '<img src="' + PictureURL + '" onclick="this.requestFullscreen()" style="width: 90%;" class="center">'; // Use the PictureURL from JSON data console.log('Image URL:', imageHTML); // Log the image URL to the console paintingDiv.innerHTML = imageHTML; } - User
I'm still getting an error that PictureURL is undefined
- Assistant
Apologies for the oversight. It seems that the
PictureURLis not accessible in theupdateImagefunction because it is defined within thefetchStreetDetailsfunction and not globally accessible.To make the
PictureURLaccessible in theupdateImagefunction, you can store it in a global variable or pass it as a parameter to theupdateImagefunction.Here's an example of updating the code to pass
PictureURLas a parameter to theupdateImagefunction:- Modify the
fetchStreetDetailsfunction to passPictureURLto theupdateImagefunction:
// Inside fetchStreetDetails function, pass PictureURL to the callback callback(FeatureID, PictureURL); - Modify the