Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
How to pass arguments to addEventListener for google maps Polygon listener function in addition to the event?

I want to send other parameters for this function in addition to the event. This is to display the information sent to the function in the pop-up when the plugins are clicked. How can I do this?

var bermudaTriangle = new google.maps.Polygon({
                        paths: triangleCoords,
                        strokeColor: "#fd7e14",
                        strokeOpacity: 0.8,
                        strokeWeight: 3,
                        fillColor: "rgba(253,126,20,0.62)",
                        fillOpacity: 0.35
                    });

bermudaTriangle.setMap(map);
area_info = {"name" : "1", "shipping_cost" : 100}

bermudaTriangle.addListener("click", showArrays);
infoWindow = new google.maps.InfoWindow();

function showArrays(event, area) {
        let contentString =
            '<div class="form-group pr-2 pl-2">' +
            '<div class="text-dark">name:</div>' +
            area.name +
            '<br>'+
            '<div class="text-dark">shipping_cost :</div>' +
            area.shipping_cost +
            '$'+
            '<br></div>'
        ;

        // Replace the info window's content and position.
        infoWindow.setContent(contentString);
        infoWindow.setPosition(event.latLng);
        infoWindow.open(map);
    }

When I send information in bermudaTriangle.addListener("click", showArrays(area_info) ) form to a function, I do not know how to send the event. And when I call the function like this bermudaTriangle.addListener("click", showArrays(bermudaTriangle, area_info) ), the event does not work properly. I try this but it still did not work out.

bermudaTriangle.addListener("click", showArrays);
bermudaTriangle.area = area_info;

and showArrays:

function showArrays(event, area) {
    let contentString =
        '<div class="form-group pr-2 pl-2">' +
        '<div class="text-dark">name:</div>' +
        event.currentTarget.name +
        '<br>'+
        '<div class="text-dark">shipping_cost :</div>' +
        event.currentTarget.shipping_cost +
        '$'+
        '<br></div>'
    ;

    // Replace the info window's content and position.
    infoWindow.setContent(contentString);
    infoWindow.setPosition(event.latLng);
    infoWindow.open(map);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The Polygon's click event listener takes only one parameter. That means the library passes no more than one parameter when it calls the function.

If the information you need is not attached to the event object, you have to retrieve it by other means.

If you need to pass the parameters yourself, you can write a higher order function:

function showArrays(area) {
  return function (event) {
    let contentString =
      '<div class="form-group pr-2 pl-2">' +
      '<div class="text-dark">name:</div>' +
      area.name +
      "<br>" +
      '<div class="text-dark">shipping_cost :</div>' +
      area.shipping_cost +
      "$" +
      "<br></div>";
    // Replace the info window's content and position.
    infoWindow.setContent(contentString);
    infoWindow.setPosition(event.latLng);
    infoWindow.open(map);
  };
}

bermudaTriangle.addListener("click", showArrays(area));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda