-2

My Project having a dropdown of states/Province of canada, I select those state based on zip code find out the city or state by google.geocoder

While i am using for canada geocoder.geocode response in french while I need the response in english.

 var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': "G1B0A3, Canada" }, 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 types = results[0].address_components[ii].types.join(",");
                        if (types.includes("locality")) {
                            $("#City").val(results[0].address_components[ii].long_name);
                            city = results[0].address_components[ii].long_name;
                        }
                        if (types == "administrative_area_level_1,political") {
                            $(".StateClass option").each(function () {
                                if ($(this).text().toUpperCase() == results[0].address_components[ii].long_name.toUpperCase()) {
                                    if (typeof (state) != "undefined") {
                                        state = $(this).val();
                                        $(".StateClass").val($(this).val());
                                    }
                                    else {
                                        $(this).attr('selected', true);
                                        state = $(this).val();
                                        $(".StateClass").val(state);
                                    }
                                }
                            });
               }
           }
        }
    }
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
Prakash
  • 3
  • 1
  • 2

2 Answers2

0

This behavior is pretty clearly spelled out in the Geocoding API docs:

language — The language in which to return results.

  • See the list of supported languages. Google often updates the supported languages, so this list may not be exhaustive.
  • If language is not supplied, the geocoder attempts to use the preferred language as specified in the Accept-Language header, or the native language of the domain from which the request is sent.
    [...]

As such, add the language option to your initial call to geocoder.geocode, specifying English (en) as the language "in which to return results":

geocoder.geocode({ 'address': "G1B0A3, Canada", 'language': 'en' }, function (results, status) {
esqew
  • 42,425
  • 27
  • 92
  • 132
  • I got the response "long_name" : "Québec",, earlier i passed the language en, but i got the same response, either passed en or not in language – Prakash Oct 31 '19 at 09:48
  • i went through the same, by supportive languages but i didnt get the response – Prakash Oct 31 '19 at 10:23
0

After lot of search i didnt get for solution to get response from google in english for few zipcodes like 'G1B0A3', i use the short_name, is in english instead of this long_name some case convert as per localize language

for shortname : results[0].address_components[ii].short_name

Prakash
  • 3
  • 1
  • 2