In this javascript why is the polyline not being added to the map when the submit button…
- User
In this javascript why is the polyline not being added to the map when the submit button is pressed ' let streetLatitude; // Define streetLatitude globally let streetLongitude; // Define streetLongitude globally
function fetchStreetDetails() { fetch("main.json") .then((response) => response.json()) .then((data) => { const streetDetails = data[0]; streetLatitude = streetDetails.StreetLatitude; streetLongitude = streetDetails.StreetLongitude; const streetHeading = streetDetails.StreetHeading; const streetPitch = streetDetails.StreetPitch; const streetPanoID = streetDetails.StreetPanoID; const StreetPoints = streetDetails.Points;
const panorama = new google.maps.StreetViewPanorama( document.getElementById("streetview"), { position: { lat: streetLatitude, lng: streetLongitude }, 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); }) .catch((error) => console.error("Error fetching data: ", error));}
fetchStreetDetails();
const startingLocation = { lat: 51.540073, lng: -0.010874 }; // London Aquatics Center coordinates
function initMap() { const zoom = 8;
const map = new google.maps.Map(document.getElementById("map"), { center: startingLocation, zoom: zoom, mapId: "DEMO_MAP_ID", });
// Function to add marker on click function addMarker(event) { const clickLocation = event.latLng; const marker = new google.maps.Marker({ position: clickLocation, map: map, draggable: true, // Set draggable to true });
// Calculate distance on marker add using Pythagorean theorem const distance = calculateDistance( clickLocation.lat(), clickLocation.lng(), streetLatitude, streetLongitude ); console.log("Distance from marker to street: " + distance + " meters"); // Add functionality after clicking the map createSubmitButton(distance);}
// Create a function to add the submit button function createSubmitButton(distance, clickLocation) { const buttonsDiv = document.getElementById("buttons");
// Check if the button already exists before creating a new one if (!document.getElementById("submit")) { const submitButton = document.createElement("button"); submitButton.id = "submit"; submitButton.textContent =
Submit;// Add event listener for the submit button (you can define the functionality here) submitButton.addEventListener("click", () => { console.log("Submit button clicked! Distance:", distance); // Add your submit logic here const message = "You are " + distance + " meters from the correct location."; // Update the 'results' div using DOM manipulation const resultsDiv = document.getElementById("results"); resultsDiv.textContent = message; // Call drawPolyline passing the clickLocation as argument drawPolyline(clickLocation); }); buttonsDiv.appendChild(submitButton); - Assistant
The issue with the code is that the
clickLocationparameter is not being passed correctly to thecreateSubmitButtonfunction, causing it to beundefined. This is why the polyline is not being added to the map when the submit button is pressed.To fix this issue, you need to ensure that the
clickLocationis passed correctly to thecreateSubmitButtonfunction. You can pass theclickLocationas an argument when callingcreateSubmitButton(distance)inside theaddMarkerfunction.Replace this line:
createSubmitButton(distance);With this line:
createSubmitButton(distance, clickLocation);By passing the
clickLocationto thecreateSubmitButtonfunction, it will be available when callingdrawPolyline(clickLocation)inside the event listener for the submit button click. This should resolve the issue and allow the polyline to be added to the map when the submit button is pressed. - User
- Assistant
In the provided code, the
marker.setDraggable(false);line is inside the event listener for the submit button click, which means it will only be executed when the submit button is clicked. This results in the marker still being draggable after the submit button has been pressed.To fix this issue, you can move the
marker.setDraggable(false);outside of the submit button event listener, so it is applied immediately after submitting the marker's location and distance. Here's how you can modify the code:// Add event listener for the submit button (you can define the functionality here) submitButton.addEventListener("click", () => { console.log("Submit button clicked! Distance:", distance); // Add your submit logic here const message = "You are " + distance + " meters from the correct location."; // Update the 'results' div using DOM manipulation const resultsDiv = document.getElementById("results"); resultsDiv.textContent = message; // Call drawPolyline passing the clickLocation as argument drawPolyline(clickLocation); // Disable dragging on the marker marker.setDraggable(false); }); // Disable dragging on marker when submitting marker.setDraggable(false); - User
when I do that I get an error that 'marker is not defined'
- Assistant
Apologies for the oversight. Since the variable is defined inside the function and not accessible outside of it, we need to make it available to the function to disable dragging on the marker.