-2

I'm using the Places Autocomplete Google API passing - input and key. I type an address (search text) which is sent to the API and all the matches (addresses) for that text is returned from which I select one. The addresses returned don't have a postal code. How can I get it? What am I doing wrong

Ryan Kozak
  • 1,091
  • 6
  • 16
  • 21
slugspeed
  • 17
  • 4
  • 1
    Can you post your code? – Ryan Kozak May 23 '18 at 16:32
  • https://stackoverflow.com/questions/34487077/how-do-i-get-the-postal-code-from-google-maps-autocomplete-api?rq=1 – Ryan Kozak May 23 '18 at 16:35
  • Possible duplicate of [how do i get the postal code from google map's autocomplete api](https://stackoverflow.com/questions/34487077/how-do-i-get-the-postal-code-from-google-maps-autocomplete-api) – Ryan Kozak May 23 '18 at 16:36
  • public function suggestions_post(){ $search = $this->post('search'); $arrContextOptions=array("ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false,),); $url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=".urlencode($search)."&types=(regions)&key=AIzaSyCTogatxbgSslrceh8ti5bKo4TACL2xwp0&libraries=places"; $json = file_get_contents($url, false, stream_context_create($arrContextOptions)); $results = json_decode($json); $this->response(['predictions' => $results->predictions]); } – slugspeed May 23 '18 at 16:38
  • 1
    Thanks @Ryankozak. I checked that link earlier and it didn't work with my code. using types=(regions) in the url did the trick. Thanks for your help. Sorry if it was a duplicate question – slugspeed May 23 '18 at 16:45
  • @slugspeed Rather than pasting code in comments you can re edit your question by clicking on the edit button below your question. Having lot of code in the comment area is hard to read here's how to https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks –  May 24 '18 at 01:35

1 Answers1

1

Please try the below code by adding your api key it's working fine for me

$(document).on('click', "#autocomplete", function () {
        var currentInp = $(this).attr("id");
        var $tabletextbox = $(this);
        var autocomplete = new google.maps.places.Autocomplete(document.getElementById(currentInp));
        autocomplete.addListener('place_changed', function () {
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                // User entered the name of a Place that was not suggested and
                // pressed the Enter key, or the Place Details request failed.
                window.alert("No details available for input: '" + place.name + "'");
                return;
            }
            for (var i = 0; i < place.address_components.length; i++) {
              if(place.address_components[i].types[0] == 'postal_code'){
                alert(place.address_components[i].long_name);
              }
            }
            $('#lat').val(place.geometry.location.lat().toFixed(8));
            $('#lng').val(place.geometry.location.lng().toFixed(8));

        });
    });
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=Your API Key&libraries=places"></script>
Lavish Tyagi
  • 223
  • 5
  • 10