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

169
Views
How do I create a working infoWindow and use event listener within it?

I am trying to create an infowindow that shows a piece of text 'this is my marker' when the map is loaded but it is not showing. I'm using the exact same code from my lecturer's powerpoint but idk where I'm going wrong.

function initialize(){
    
    let mapDiv = document.getElementById('map');
    
    let mapOptions = {
        center: new google.maps.LatLng(Coords.lat,Coords.lng),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.HYBRID
    };
    
    let marker = new google.maps.Marker({
        position: new google.maps.LatLng(40.72,-74.00),
        map: map,
        title: 'I am here!',
        icon: svgMarker
    });
    
    var infoWindow = new google.maps.InfoWindow({content: 'This is my marker'});
    google.maps.event.addListener(marker, 'click', function()
    {
        infoWindow.open(map,this);
    });
    
        map = new google.maps.Map(document.getElementById("map"), mapOptions);
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have the sequence slightly wrong. You are attempting to add both the marker & the infoWindow before you have created the map. Perhaps if you re-sequence your code a little like this:

/* assumed that a global variable exists */
let Coords={
    lat:41,
    lng:-75
};


function initialize(){
    let options = {
        center: new google.maps.LatLng( Coords.lat,Coords.lng ),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.HYBRID
    };

    let map = new google.maps.Map( document.getElementById("map"), options );
  
    let marker = new google.maps.Marker({
        position: new google.maps.LatLng(40.72,-74.00),
        map: map,
        title: 'I am here!'
        /*,
        icon: svgMarker
        */
    });
  
    let infoWindow = new google.maps.InfoWindow( { content:'This is my marker' } );
        infoWindow.open( map, marker );
      
    google.maps.event.addListener(marker, 'click', function(e) {
        infoWindow.open(map,this);  
    });
};

To get the infoWindow to appear when the map is loaded you should call the open method and if you supply the previously created marker as the second argument it will appear in the correct location.

about 4 years ago · Juan Pablo Isaza Report
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!