I have a button with the id 'submitButton'. In this Javascript move all the events from…
- User
I have a button with the id 'submitButton'. In this Javascript move all the events from the map.on function into a click event initiated by pressing the submitButton, except for the variable 'var userMarker = L.marker(e.latlng).addTo(map);', which should remain in the map.on function. The result is that the map.on function should add a marker to the map but the other events happen only when the submitButton is pressed. - map.on("click", function (e) { // Add user marker to the array var userMarker = L.marker(e.latlng).addTo(map); userMarkers.push(userMarker.getLatLng());
const marker = L.marker(e.latlng).addTo(map); const distance = L.latLng(e.latlng).distanceTo(referencePoint); map.off("click"); // Convert meters to miles: const distanceInMiles = distance * 0.000621371; myDiv.innerHTML = `You clicked ${distanceInMiles.toFixed(2)} miles from ${ locationNames[referencePoint] }`; // Update total distance with clicked marker's distance totalDistance += distanceInMiles; roundDistances.push(distanceInMiles); // Add distance to the roundDistances array // connect user marker to correct location const polyline = L.polyline([e.latlng, referencePoint], { color: "black", }).addTo(map); // Put marker on correct location const stationMarker = L.marker(referencePoint, { icon: greenIcon }).addTo( map ); - Assistant
document.getElementById('submitButton').addEventListener('click', function() { const e = map.getCenter(); var userMarker = L.marker(e.latlng).addTo(map); userMarkers.push(userMarker.getLatLng());
const marker = L.marker(e.latlng).addTo(map); const distance = L.latLng(e.latlng).distanceTo(referencePoint); // Convert meters to miles: const distanceInMiles = distance * 0.000621371; myDiv.innerHTML = `You clicked ${distanceInMiles.toFixed(2)} miles from ${ locationNames[referencePoint] }`; // Update total distance with clicked marker's distance totalDistance += distanceInMiles; roundDistances.push(distanceInMiles); // Add distance to the roundDistances array // connect user marker to correct location const polyline = L.polyline([e.latlng, referencePoint], { color: "black", }).addTo(map); // Put marker on correct location const stationMarker = L.marker(referencePoint, { icon: greenIcon }).addTo( map );