Access JS function

Asked

Viewed 50 times

0

How can I call a JS function inside a click event, and this function is inside another?

Follows the code:

    function setPin(geocoder, resultsMap, address, i) {
      geocoder.geocode({
        'address': address
      }, function(results, status) {
           if (status === google.maps.GeocoderStatus.OK) {
             var lat  = results[0].geometry.location.lat(); //obtem latitude
             var long = results[0].geometry.location.lng(); //obtem longitude

             var cod  = names[i].split(',')[0];
             var name = names[i].split(',')[1];

             var novaInfoWindow = criaInfoWindow(cod, name, address);

             var marker = new google.maps.Marker({
                 map: resultsMap,
                 position: results[0].geometry.location
             });

             marker.addListener('click', function() {
                novaInfoWindow.open(resultsMap, marker);
             });

             function checkInPolygon(polygon) {
                myresult = google.maps.geometry.poly.containsLocation(results[0].geometry.location, polygon);
             }
          } //status
      }); //geocoder.geocode
    }//setPin

The click event calling the function looks like this: (remembering that the event is inside another function - poly() )

polygon.addListener('click', checkInPolygon(polygon)); 

The following error returns me:

Uncaught ReferenceError: checkInPolygon is not defined

I know the reason for this mistake is because the function checkInPolygon is within two other functions, but I don’t have enough practice to know how to access this function. Any idea?

  • It is necessary somehow to invoke setPin and then invoke checkInPolygon, but in your case she’s still in geocoder.geocode. Why do I need her straight?

  • @Lucascosta this function will check for me if the locations that are being shown on the map are inside a polygon that was drawn on the map. That is why you are inside another function: there is a function where you create the Poligono (poly()), and after it is completed (event polygoncomplete), recovers its vertices and within this event, there is still the event of click that would enable me to know "who" is inside the Opoligono through this function that I am trying to access...

1 answer

0


Acebei getting what he wanted!

As the desired function (chekInPolygon()) was encapsulated inside two other (setPin() and geocoder.geocode()), ended up creating a google maps event within the same if, performing what was expected.

Before

function setPin(geocoder, resultsMap, address, i) {
      geocoder.geocode({
        'address': address
      }, function(results, status) {
           if (status === google.maps.GeocoderStatus.OK) {
             var lat  = results[0].geometry.location.lat(); //obtem latitude
             var long = results[0].geometry.location.lng(); //obtem longitude

             var cod  = names[i].split(',')[0];
             var name = names[i].split(',')[1];

             var novaInfoWindow = criaInfoWindow(cod, name, address);

             var marker = new google.maps.Marker({
                 map: resultsMap,
                 position: results[0].geometry.location
             });

             marker.addListener('click', function() {
                novaInfoWindow.open(resultsMap, marker);
             });

             function checkInPolygon(polygon) {
                myresult = google.maps.geometry.poly.containsLocation(results[0].geometry.location, polygon);
             }
          } //status
      }); //geocoder.geocode
}//setPin

Afterward

function setPin(geocoder, resultsMap, address, i) {
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            var lat  = results[0].geometry.location.lat(); //obtem latitude
            var long = results[0].geometry.location.lng(); //obtem longitude

            var cod  = names[i].split(',')[0];
            var name = names[i].split(',')[1];

            var novaInfoWindow = criaInfoWindow(cod, name, address);

            var marker = new google.maps.Marker({
                map: resultsMap,
                position: results[0].geometry.location
            });

            marker.addListener('click', function() {
                novaInfoWindow.open(resultsMap, marker);
            });

            google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
                polygon.addListener('click', function(){
                    myresult = google.maps.geometry.poly.containsLocation(results[0].geometry.location, polygon);
                    if (myresult == true){
                        var respFim = results[0].geometry.location;

                        document.querySelector("#dash").innerHTML = respFim;
                    } //myresult == true

                    carrega();
                }); //click event
            }); //polygon complete event 
        } //status
    }); //geocoder.geocode
 } //setPin

I could not access the function, but returned from a much easier method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.