Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
How to Set Focus Infowindow Marker Google Maps?

I want to display a location on google maps whose data is fetched from a database. So far, I've been able to do it. But, there is still a problem when displaying the infowindow as shown below.

enter image description here

When I click on one of the markers, it can bring up an infowindow with data from that marker.

enter image description here

And when I click on another marker, it also displays data from that marker. But the infowindow still focuses on the same marker.

How to fix it? the following code that I have made.

<script>
    function initMap() {
        const map = new google.maps.Map(document.getElementById("map"), {
            zoom: 10,
            center: { lat: -6.261493, lng: 106.810600 },
    });
    
    setMarkers(map);
    }
    
    const locations = @php print_r(json_encode($markers)) @endphp;
    
    function setMarkers(map) {
    
    for (let i = 0; i < locations.length; i++) {
        
        const location  = locations[i];

        let html = "";
        html += `<div id="content-${location[0]}">`;
        html += `<div id="siteNotice">`;
        html += `</div>`;
        html += `<h1 id="firstHeading" class="firstHeading">${location[1]}</h1>`;
        html += `<div id="bodyContent">`;
        html += `<p>${location[2]}</p>`;
        html += `<a href="reports/show/${location[0]}" class="btn btn-dark btn-sm">Detail</a>`;
        html += `</div>`;
        html += `</div>`;

        const infowindow = new google.maps.InfoWindow({
            content: html,
        });

        marker = new google.maps.Marker({
        position: { lat: parseFloat(location[4]), lng: parseFloat(location[5]) },
        map,
        title: location[3]
        });

        marker.addListener("click", () => {
            infowindow.open({
            anchor: marker,
            map,
            shouldFocus: true,
            });
        });

        console.log(location);
    
        }
    }
</script>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!