google maps Geocoder Returns 以法语回应加拿大,而我需要英文回应

google maps Geocoder Returns response for canada in french, while I need the response in english

我的项目有加拿大 states/Province 的下拉菜单,我 select 那些 州根据邮政编码通过 google.geocoder[= 找出城市或州21=]

虽然我在加拿大使用 geocoder.geocode 响应 french 而我需要 响应英语.

 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);
                                    }
                                }
                            });
               }
           }
        }
    }
}

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.
    [...]

因此,将 language 选项添加到对 geocoder.geocode 的初始调用中,指定英语 (en) 作为语言 "in which to return results":

geocoder.geocode({ 'address': "G1B0A3, Canada", 'language': 'en' }, function (results, status) {

经过大量搜索后,我没有找到解决方案来获得 google 的英文回复,但我使用的 short_name 是英文的,而不是这个long_name 某些情况下根据本地化语言转换

短名称:结果[0].address_components[ii].short_name