in this javascript remove the map click even after a marker has been added to the map '…
- User
in this javascript remove the map click even after a marker has been added to the map ' 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"); 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; // Createpolyline on marker add drawPolyline(clickLocation); // Add functionality after clicking the map createSubmitButton(distance);}
// Create a function to add the submit button function createSubmitButton(distance) { 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 }); buttonsDiv.appendChild(submitButton); } - Assistant
To remove the click event after a marker has been added to the map, you can modify the
addMarkerfunction to remove the click listener from the map. Here's the updated code:// 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 }); // Remove the click listener after adding a marker google.maps.event.removeListener(clickListener); // 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"); 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; // Createpolyline on marker add drawPolyline(clickLocation); // Add functionality after clicking the map createSubmitButton(distance); } // Add click listener to the map const clickListener = map.addListener("click", addMarker); - User
- Assistant
The issue with the polyline not being drawn when the submit button is pressed is that the
clickLocationvariable is not accessible within thecreateSubmitButtonfunction. You can pass the as an argument to the function to make it available for drawing the polyline. Here's how you can modify the code: - User
how do I stop the marker being draggable after the submit button is pressed
- Assistant
To stop the marker from being draggable after the submit button is pressed, you can update the marker's
draggableproperty tofalsewithin the click event listener of the submit button. Here's how you can do it: - User
- Assistant
- User
- Assistant
To change the location of where the polyline is drawn from and where the distance is measured from to the position of the marker when the submit button is pressed, you will need to update the
drawPolylinefunction to use the marker's position instead of theclickLocationpassed from thecreateSubmitButtonfunction. Here's how you can modify the code:// Function to draw polyline between marker and street location function drawPolyline() { const markerPosition = marker.getPosition(); // Get the current position of the marker const polyline = new google.maps.Polyline({ path: [markerPosition.toJSON(), { lat: streetLatitude, lng: streetLongitude }], strokeColor: "#FF0000", // red color strokeWeight: 2, map: map, }); } - User
can you update the distance calculation function in the same way, so that marker.getPosition(); is used to measure between the marker and the street location
- Assistant
Certainly! To update the distance calculation function to use the position of the marker () to calculate the distance between the marker and the street location, you can modify the function as follows:
- User
- Assistant
The error
markerPosition.lat is not a functionoccurs because themarkerPositionobtained frommarker.getPosition()is not a LatLng object, and therefore it does not have thelat()andlng()methods to retrieve the latitude and longitude values.To resolve this issue, you can directly access the
lat()andlng()methods on themarkerPositionobject without the need for callinglat()andlng()methods on it. Here is the updatedcalculateDistancefunction:function calculateDistance(markerPosition, streetLatitude, streetLongitude) { const lat1 = markerPosition.lat(); const lng1 = markerPosition.lng(); const deltaLat = (streetLatitude - lat1) * Math.PI / 180; const deltaLng = (streetLongitude - lng1) * Math.PI / 180; const earthRadius = 6371e3; // meters const a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2); const b = Math.cos((lat1 * Math.PI) / 180) * Math.cos((streetLatitude * Math.PI) / 180) * Math.sin(deltaLng / 2) * Math.sin(deltaLng / 2); const c = 2 * Math.atan2(Math.sqrt(a + b), Math.sqrt(1 - a - b)); // Calculate distance using the Haversine formula const distance = earthRadius * c; // Round the distance to the nearest meter const roundedDistance = Math.round(distance); return roundedDistance; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.