Search code examples
javascriptgoogle-mapsgoogle-geocoder

TypeError: a is undefined in Google Maps geocoder


I am trying to create some code that would allow an infowindow with reverse geocoded locality and country to pop up wherever the user clicks. I actually got the reverse geocoding part to work. However when fiddling around with the code it started giving the error message "TypeError: a is undefined in Google Maps geocoder".

I have tried reverting the code back to where it used to work, but I can't seem to get it working again. Removing the entire function doesn't do anything; geocoder.geocode({'location': latLng}) immediately gives me the error message. I checked that the latitude and longitude being fed into geocoder are correct.

var map;

    //initialize the map
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: {
                lat: 0,
                lng: 0
            },
            zoom: 2
        });
    }

    initMap();



    function placeMarker(latLng, map) {
        //place marker
        var marker = new google.maps.Marker({
            position: latLng,
            map: map
        });


        //reverse geocode marker location
        var geocoder = new google.maps.Geocoder;

        geocoder.geocode({'location': latLng}), function(results, status) {
                ...
                };

            };
        ...

    //tie placing markers and opening info windows to the mouse click
    map.addListener('click', function(e) {
        placeMarker(e.latLng, map);

    });

Solution

  • You have placed ) incorrectly. After ...cation': latLng} you are closing arguments list, so the function you are trying to pass as callback declared syntaxically wrong. Move you ) after ...cation': latLng} to the end of you callback. Like this:

    geocoder.geocode({'location': latLng}, function(results, status) {
        ...
    })