Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map places API getPlace() only return name attribute for some addresses

I have an address search box using the google map autocomplete library:

var autocompleter = new google.maps.places.Autocomplete(item);

There is an odd occasion that an address only returns a name attribute:

Object {name: "138 Manukau Road, Pukekohe, New Zealand"} 

But other addresses are giving more data, such as:

Object {address_components: Array[7], adr_address: "<span class="street-address">430 Queen St</span>, …n>, <span class="country-name">New Zealand</span>", formatted_address: "430 Queen St, Auckland, Auckland 1010, New Zealand", geometry: Object, icon: "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png"…}address_components: Array[7]adr_address: "<span class="street-address">430 Queen St</span>, <span class="extended-address">Auckland</span>, <span class="locality">Auckland</span> <span class="postal-code">1010</span>, <span class="country-name">New Zealand</span>"formatted_address: "430 Queen St, Auckland, Auckland 1010, New Zealand"geometry: Objecthtml_attributions: Array[0]icon: "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png"id: "00fce9b1c43ac960068949cbf32eecb587b0b020"name: "430 Queen St"place_id: "ChIJQfHW8OVHDW0RyHgQRLy8fKc"reference: "CqQBlgAAAIDnkWNQ4cmU624FV6l_bAxmI27czZoytmzrrEWVaXgR5LcZuFqt1cL3WIMzoWhmZNhftRzhLUVwpFjqmw3qwKIqugj02HrvU5x6PtUvepPNPV-08pin_PvRU-__mMMH3N2vILIOLM_AnYFMqNG5MArF4ChZXJxZj6vk7PI3ORJe1W6QjIXoPgesL379E4WUCjrZ0fjv3KgqzB-G4f-8A5MSEN5S47-QZqkY5sl37cIQFWQaFLg4InSVLpYGg8n1gGO958TcA4UK"scope: "GOOGLE"types: Array[1]url: "https://maps.google.com/maps/place?q=430+Queen+St,+Auckland,+Auckland+1010,+New+Zealand&ftid=0x6d0d47e5f0d6f141:0xa77cbcbc441078c8"vicinity: "Auckland"__proto__: Object

I have found a similar issue which raised by someone in 2012, and looks like it has not been attended.

like image 270
James Lin Avatar asked Oct 29 '14 03:10

James Lin


People also ask

How do I get my city name in Google Places API?

2) Make another web-service call to https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1. This will return a JSON which contains address_components . Looping through the types to find locality and postal_code can give you the city name and postal code.


1 Answers

This problem was happening intermittently for me and usually out of the blue.

Turns out that if you get a result from Autocomplete with only a name property, you can use the google.maps.places.AutocompleteService to finish the job.

For example, call this if you only get a name back (sending the input element in el)

        function getPlace(result, el, callback) {
            var autocompleteService = new google.maps.places.AutocompleteService();
            if (result.name.length > 0) {
                var d = { input: result.name, offset: result.name.length };
                autocompleteService.getPlacePredictions(d, function (list, status) {
                    if (list == null || list.length == 0) callback(null);

                    var placesService = new google.maps.places.PlacesService(el);
                    var ref = { 'reference': list[0].reference }

                    placesService.getDetails(ref, function (detailsResult, placesServiceStatus) {
                        if (placesServiceStatus == google.maps.GeocoderStatus.OK) {
                            callback(detailsResult);
                        }
                        else {
                            callback(null);
                        }
                    });
                });
            }
        }

This helped me a lot http://plnkr.co/edit/GF3nM3XfYX9El2w11pGo?p=preview

like image 165
keef Avatar answered Nov 05 '22 18:11

keef