4

I am facing issue regarding geocoding, whenever I put zip code in my text field its only fetch me city state and country of USA. When I put any other zip code it is not working. I think there is issue regarding my script source . Can anybody help me regarding this matter?

<script language="javascript" src="https://maps.google.com/maps/api/js? 
       sensor=false&key=my_api">
</script>
<script>
    function getLocation() {
        getAddressInfoByZip(document.getElementById('zip_id').value);
    }

    function response(obj) {
        console.log(obj);
    }

    function getAddressInfoByZip(zip) {
        if (zip.length >= 5 && typeof google != 'undefined') {
            var addr = {};
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({
                'address': zip
            }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results.length >= 1) {
                        for (var ii = 0; ii < results[0].address_components.length; ii++) {
                            var street_number = route = street = city = state = zipcode = country = formatted_address = '';
                            var types = results[0].address_components[ii].types.join(",");
                            if (types == "street_number") {
                                addr.street_number = results[0].address_components[ii].long_name;
                            }
                            if (types == "route" || types == "point_of_interest,establishment") {
                                addr.route = results[0].address_components[ii].long_name;
                            }
                            if (types == "sublocality,political" || types == "locality,political" || types == "neighborhood,political" || types == "administrative_area_level_3,political") {
                                addr.city = (city == '' || types == "locality,political") ? results[0].address_components[ii].long_name : city;
                            }
                            if (types == "administrative_area_level_1,political") {
                                addr.state = results[0].address_components[ii].short_name;
                            }
                            if (types == "postal_code" || types == "postal_code_prefix,postal_code") {
                                addr.zipcode = results[0].address_components[ii].long_name;
                            }
                            if (types == "country,political") {
                                addr.country = results[0].address_components[ii].long_name;
                            }
                        }
                        document.getElementById('country').value = addr.country;
                        document.getElementById('state').value = addr.state;
                        document.getElementById('city').value = addr.city;
                        addr.success = true;
                        for (name in addr) {
                            console.log('### google maps api ### ' + name + ': ' + addr[name]);
                        }
                        response(addr);
                    }
                    else {
                        response({
                            success: false
                        });
                    }
                }
                else {
                    response({
                        success: false
                    });
                }
            });
        }
        else {
            response({
                success: false
            });
        }
    }
</script>
ThisClark
  • 14,352
  • 10
  • 69
  • 100
Ahsan Bilal
  • 59
  • 1
  • 3
  • 1
    Just to test try setting a region and see if it works -```https://developers.google.com/maps/documentation/javascript/localization``` You may just need to add a drop down with different region codes. – demo7up Nov 23 '19 at 04:02
  • Using the example front-end form at https://developers.google.com/maps/documentation/geocoding/intro, `75004` gives no results but `75004 France` works. Can you give us an example 'zip' to work with? It seems like you are hoping to just throw a few numbers at Google's api and have them determine a lot... – JasonB Nov 23 '19 at 04:02
  • 1
    @JasonB hes also using the maps api ```https://maps.google.com/maps/api/js?``` & not the geocode api ```https://maps.googleapis.com/maps/api/geocode/%outputFormat%?%parameters%``` – demo7up Nov 23 '19 at 04:06
  • can you tell me exact api script for postal code – Ahsan Bilal Nov 23 '19 at 15:54
  • that can check all postal code – Ahsan Bilal Nov 23 '19 at 15:54

0 Answers0