JMD
JMD

Reputation: 89

Why Google map getPlace() always returning Undefined object?

This is the function I have used for getPlace() which always returning undefined object:

var element = document.getElementById('hdnOldAddress');
var AutoCompleteAddress2;
element.value = 'Marketfair Mall 1916 Skibo Road  Fayetteville  NC 28314';

autocomplete2 = new google.maps.places.Autocomplete(element);


autocomplete2.addListener('place_changed', function () {
    var place2 = autocomplete2.getPlace();
    if (place2.address_components) {
        AutoCompleteAddress2 = [
            (place2.address_components[0] && place2.address_components[0].short_name || ''),
            (place2.address_components[1] && place2.address_components[1].short_name || ''),
            (place2.address_components[2] && place2.address_components[2].short_name || ''),
            (place2.address_components[3] && place2.address_components[3].short_name || ''),
            (place2.address_components[4] && place2.address_components[4].short_name || ''),
            (place2.address_components[5] && place2.address_components[5].short_name || ''),
            (place2.address_components[6] && place2.address_components[6].short_name || ''),
            (place2.address_components[7] && place2.address_components[7].short_name || ''),
            (place2.address_components[8] && place2.address_components[8].short_name || ''),
            (place2.address_components[9] && place2.address_components[9].short_name || ''),
            (place2.address_components[10] && place2.address_components[10].short_name || ''),
            (place2.address_components[11] && place2.address_components[11].short_name || ''),
            (place2.address_components[12] && place2.address_components[12].short_name || ''),
            (place2.address_components[13] && place2.address_components[13].short_name || ''),
            (place2.address_components[14] && place2.address_components[14].short_name || ''),
        ].join(' ');
    }
});
google.maps.event.trigger(autocomplete2, 'place_changed');

Edit

Replaced this.getPlace() with autocomplete2.getPlace()

Upvotes: 0

Views: 2696

Answers (2)

JMD
JMD

Reputation: 89

This is what I've used alternate to above. Instead of using Autocomplete() I've used AutocompleteService() which returns predicted places and using that place address I passes it to geocoding and the map shows location as expected.

var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions({ input: 'Marketfair Mall 1916 Skibo Road  Fayetteville  NC 28314' }, function (predictions, status) {                    
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        matchAddress = predictions[0].structured_formatting.secondary_text; 
        //Add further processing for geocoding for found address.
    }
});

Upvotes: 1

Emmanuel Delay
Emmanuel Delay

Reputation: 3679

EDIT: Never mind, I'm wrong. In this code, this = autocomplete2.

The issue is that place_changed is not triggered ...


One of the problems you encounter is that "this" is not what you want it to be. You cannot use "this" (meaning the instance of Autocomplete) in a function that handles an event (on place_changed). Inside those event functions the this variable means the element that was affected (clicked on, hovered over, ...)

try this:

replace var place2 = this.getPlace(); by var place2 = autocomplete2.getPlace();

Let me know if this fixed it (otherwise we need to look further)

Upvotes: 1

Related Questions