最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set the language of google autocomplete form to English? - Stack Overflow

programmeradmin2浏览0评论

I'm following this Google example of places autoplete adressform.

The problem is that if I insert "Rome, Italy" the form returns in City "Roma" and in Country "Italia" (the language is Italian). I don't want the italian name, I want the English name. In the same example, if I insert "Roma, Italia" or "Rome, Italy" I want the form returns in City "Rome" and in Country "Italy".

What should I do for setting the language of the form only in English?

Here the javascript part:

var placeSearch, autoplete;
var ponentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};

function initAutoplete() {
  // Create the autoplete object, restricting the search to geographical
  // location types.
  autoplete = new google.maps.places.Autoplete(
      /** @type {!HTMLInputElement} */(document.getElementById('autoplete')),
      {types: ['geocode']});

  // When the user selects an address from the dropdown, populate the address
  // fields in the form.
  autoplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
  // Get the place details from the autoplete object.
  var place = autoplete.getPlace();

  for (var ponent in ponentForm) {
    document.getElementById(ponent).value = '';
    document.getElementById(ponent).disabled = false;
  }

  // Get each ponent of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_ponents.length; i++) {
    var addressType = place.address_ponents[i].types[0];
    if (ponentForm[addressType]) {
      var val = place.address_ponents[i][ponentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
}

// Bias the autoplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autoplete.setBounds(circle.getBounds());
    });
  }
}

And here the api with the key:

<script src="=[API key]&libraries=places&callback=initAutoplete" async defer></script>

Thanks in advance

I'm following this Google example of places autoplete adressform.

The problem is that if I insert "Rome, Italy" the form returns in City "Roma" and in Country "Italia" (the language is Italian). I don't want the italian name, I want the English name. In the same example, if I insert "Roma, Italia" or "Rome, Italy" I want the form returns in City "Rome" and in Country "Italy".

What should I do for setting the language of the form only in English?

Here the javascript part:

var placeSearch, autoplete;
var ponentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};

function initAutoplete() {
  // Create the autoplete object, restricting the search to geographical
  // location types.
  autoplete = new google.maps.places.Autoplete(
      /** @type {!HTMLInputElement} */(document.getElementById('autoplete')),
      {types: ['geocode']});

  // When the user selects an address from the dropdown, populate the address
  // fields in the form.
  autoplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
  // Get the place details from the autoplete object.
  var place = autoplete.getPlace();

  for (var ponent in ponentForm) {
    document.getElementById(ponent).value = '';
    document.getElementById(ponent).disabled = false;
  }

  // Get each ponent of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_ponents.length; i++) {
    var addressType = place.address_ponents[i].types[0];
    if (ponentForm[addressType]) {
      var val = place.address_ponents[i][ponentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
}

// Bias the autoplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autoplete.setBounds(circle.getBounds());
    });
  }
}

And here the api with the key:

<script src="https://maps.googleapis./maps/api/js?key=[API key]&libraries=places&callback=initAutoplete" async defer></script>

Thanks in advance

Share Improve this question edited Mar 27, 2016 at 18:11 fabius asked Mar 22, 2016 at 16:50 fabiusfabius 551 silver badge8 bronze badges 2
  • 1 It's not a good idea to publish your API key. I would remove it. – Joachim Rohde Commented Mar 22, 2016 at 16:53
  • @JoachimRohde But when i will upload the website the key will be public, or not? – fabius Commented Mar 22, 2016 at 16:56
Add a ment  | 

1 Answer 1

Reset to default 8

You can change the language by adding &language=[language here] to the script tag. For English, that would be: &language=en

An example:

<script src="https://maps.googleapis./maps/api/js?key=YOUR_API_KEY&language=en"
type="text/javascript">
</script>

This is described here and there is a working example here.

发布评论

评论列表(0)

  1. 暂无评论