Steven Dang
Steven Dang

Reputation: 182

How to fix the wrong address fill in text input after select from Google Places autocomplete?

I'm using Google Places API with autocomplete to make a form with address for user. The autocomplete show up the list of address and it contains an address that i want to choose, but after i choose the option, the address filled in input text with a wrong address string. As i see, it's not wrong address number, it's also wrong the street name, when i console the getPlace() on event place_changed the address also be changed like the google call the api for one more time.

So is this a bug of Google API or something wrong with my code?

*EDIT: Here is my directive code for handle google place autocomplete:

app.directive('googleplace', function () {
    return {
        restrict: 'A',
        link: function ($scope, element, attrs, model) {
            $scope.gPlace = new google.maps.places.Autocomplete(element[0], {types: ['geocode']});
            google.maps.event.addListener($scope.gPlace, 'place_changed', function () {
                var position = $scope.gPlace.getPlace();
                if (!position.geometry) {
                    alert("Address is not available: ' " + position.name + " '");
                    return;
                }
                if (map && marker) {
                    if (position.geometry.viewport) {
                        map.fitBounds(position.geometry.viewport);
                    } else {
                        map.setCenter(position.geometry.location);
                    }
                    marker.setPosition(position.geometry.location);
                } else {
                    initMap(position.geometry.location.lat(), position.geometry.location.lng());
                }
                updatePosition(position.geometry.location.lat(), position.geometry.location.lng(), input_count, element[0].id);
            });
        }
    }
});

Example: I type in the input an address string 55a 3/2, Ninh Kiều, Cần Thơ, Vietnam, the autocomplete also have that result in dropdown list, but when i choose that address, the Autocomplete fill a wrong address into input text. The address string become 55 Mạc Thiên Tích, Xuân Khánh, Ninh Kiều, Cần Thơ, Vietnam.

Upvotes: 1

Views: 1928

Answers (1)

evan
evan

Reputation: 5701

This behavior is also shown in Google's Autocomplete example https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

It looks like these 2 addresses point to the same place ID, so it may be an API issue. I recommend you send feedback to Google Maps, see https://support.google.com/maps/answer/3094045?hl=en&ref_topic=3093612

Hope this helps!

Upvotes: 4

Related Questions