in this javascript I have removed the click listener after the first marker is added to the map 'google.maps.event.removeListener(clickListener);'. When more markers are added to the map for the next Street Views I want to remove the clickListener again. - ' 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
function 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 map = new google.maps.Map(document.getElementById('map'), mapOptions);
// Function to add marker on click
function addMarker(event) {
const clickLocation = event.latLng;
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);
// Add functionality after clicking the map
createSubmitButton(clickLocation);
}
Assistant
To remove the click listener after adding a marker and to allow adding additional markers on the map for the next Street Views, you can make the following modifications to the code:
Declare the variable globally to store the click listener for adding markers in the next Street View.
// 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;
submitButton.className = "button";
// Add event listener for the submit button (you can define the functionality here)
submitButton.addEventListener("click", () => {
console.log("Submit button clicked!");
// Create the new button
const nextButton = document.createElement("button");
nextButton.id = "nextButton";
nextButton.textContent = "Next"; // Customize button text as needed
nextButton.className = "nextbutton";
// Add event listener for the new button (optional, if needed)
nextButton.addEventListener("click", () => {
// Handle ‘nextButton’ click here
console.log("Next button clicked!");
buttons.removeChild(nextButton);
const wheremessage = "Next location. Where is this?";
// Update the 'results' div using DOM manipulation
const resultsDiv = document.getElementById("results");
resultsDiv.textContent = wheremessage;
// Check if there is next entry in the data
if (data && currentIndex < data.length - 1) {
currentIndex++; // Update the index to get the next street view details
const nextStreetDetails = data[currentIndex];
streetLatitude = nextStreetDetails.StreetLatitude;
streetLongitude = nextStreetDetails.StreetLongitude;
streetHeading = nextStreetDetails.StreetHeading;
streetPitch = nextStreetDetails.StreetPitch;
streetPanoID = nextStreetDetails.StreetPanoID;
const StreetPoints = nextStreetDetails.Points;
const distance = earthRadius * c; // Round the distance to nearest meter using Math.round()
const roundedDistance = Math.round(distance);
return roundedDistance;
} // 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,
});
// Set map bounds to encompass marker and polyline
const bounds = new google.maps.LatLngBounds(); // Use google.maps here
bounds.extend(markerPosition);
bounds.extend(polyline.getPath().getAt(1));
map.fitBounds(bounds);
}
}'
nextClickListener
Update the addMarker function to remove the current click listener and add the nextClickListener instead.
Update the functionality of the nextButton click event to remove the nextClickListener after adding a marker.
Here's the modified code with the above changes:
javascript
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 clickListener; // Define clickListener globally
let nextClickListener; // Define nextClickListener globally
function fetchStreetDetails() {
// Fetch street details
}
fetchStreetDetails();
function initMap() {
// Map initialization code
// Function to add marker on click
function addMarker(event) {
const clickLocation = event.latLng;
marker = new google.maps.Marker({
position: clickLocation,
map: map,
draggable: true,
});
// Remove the click listener after adding a marker
google.maps.event.removeListener(clickListener);
// Add functionality after clicking the map
createSubmitButton(clickLocation);
// Add the nextClickListener for the next Street View
nextClickListener = map.addListener("click", addMarker);
}
// Create a function to add the submit button
function createSubmitButton(distance, clickLocation) {
// Create submit button and functionality
}
// Add click listener to the map
clickListener = map.addListener("click", addMarker);
// Calculate distance function
// Function to draw polyline between marker and street location
function drawPolyline() {
// Draw polyline function
}
}
With these modifications, you should now be able to remove the click listener after the first marker is added, as well as remove the nextClickListener when adding markers for the next Street Views.