Access JS function

0

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

Follow 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 is 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 error is because the checkInPolygon function is inside two other functions, but I do not have enough practice to know how to access this function. Any ideas?

    
asked by anonymous 24.05.2017 / 19:35

1 answer

0

Acebei getting what he wanted!

As the desired function ( chekInPolygon() ) was encapsulated within another two ( setPin() and geocoder.geocode() ), I ended up creating a google maps event within the same, doing 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

Then

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 was unable to access the function, but returned from a much easier method.

    
25.05.2017 / 21:37