1

I want to visualize the map of an italian region using three markers that on mouse over should show the name of the city. This is my Javascript code:

function initMap() {
    
    var options = {
        center: {lat: 46.279, lng: 13.1368},
        zoom: 10
    }

    map = new google.maps.Map(document.getElementById('map'), options);

    function addMarker(property) {

        let marker = new google.maps.Marker({
            position: property.location,
            map: map
        });

        if(property.content) {

            detailWindow = new google.maps.InfoWindow({
                content: property.content
            });
            
        }

        marker.addListener("mouseover", () =>{
            detailWindow.open(map, marker);
        })

        
    }
    
    addMarker({location: {lat: 46.21251, lng: 13.21514}, content: '<h2>Tarcento</h2>'});
    addMarker({location: {lat: 46.4079, lng: 13.3088}, content: '<h2>Chiusaforte</h2>'});
    addMarker({location: {lat: 46.15714, lng: 13.00726}, content: '<h2>San Daniele</h2>'});
}

As you can see, I created three markers by calling the function addMarker located inside the parent function initMap. Everything works fine except for the message that appears when I put the mouse over a marker. In fact in every marker on mouse over appears the text "San Daniele" even when should appear another name city. Can help?

Michele Della Mea
  • 966
  • 2
  • 16
  • 35
  • 1
    Have you tried passing the event into the callback function and using the .target to get the element being moused over? – dale landry Mar 14 '21 at 21:26
  • 1
    Duplicate of [Google maps - multiple markers with unique infowindows](https://stackoverflow.com/questions/56869829/google-maps-multiple-markers-with-unique-infowindows) – MrUpsidown Mar 14 '21 at 21:34
  • 1
    Your are missing one line in the `addMarker` function `mouseover` listener function: `detailWindow.setContent(property.content);` [working fiddle](https://jsfiddle.net/geocodezip/5tf73r8d/1/) – geocodezip Mar 15 '21 at 00:58
  • Thanks geo that was the solution... but I cannot accept your answer! :D – Michele Della Mea Mar 15 '21 at 08:52

0 Answers0