Google地理位置可以获得不同语言的相同数据

时间:2017-02-03 15:41:38

标签: javascript google-maps google-maps-api-3 google-places-api google-geolocation

我在注册/登录脚本中使用Googles API中的以下代码来获取用户的地址:

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

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

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

// [START region_fillform]
function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();
  
  /*document.getElementById('place_id').value = place.place_id;
  document.getElementById('place_id').disabled = false;*/

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    /*document.getElementById(component).disabled = false;*/
  }

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

// [START region_geolocation]
// Bias the autocomplete 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
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
// [END region_geolocation]
<label class="tooltip" for="autocomplete">Hier Adresse eingeben:</label>
      			<input type="text" id="autocomplete" placeholder="Adresse hier eingeben" onFocus="geolocate()" />
                <label for="street_number">Strasse / Hausnummer:</label>
                <input type="text"  name="form_street" id="route" readonly       placeholder="Strasse wird automatisch ausgefüllt" />
                <input type="text"  name="form_street_number" id="street_number" readonly />
                <label  for="locality">PLZ / Stadt:</label>
                <input type="text"  name="form_postal_code" id="postal_code" readonly placeholder="PLZ autom. ausgefüllt" />
                <input type="text"  name="form_town" id="locality" readonly  placeholder="Ort wird automatisch ausgefüllt" />
                <label  for="administrative_area_level_1">Bundesland:</label>
                <input type="text"  name="form_state" id="administrative_area_level_1" readonly placeholder="Bundesland wird automatisch ausgefüllt" />
                <label for="postal_code">Land:</label>
                <input type="text" name="form_country" id="country" readonly placeholder="Land wird automatisch ausgefüllt" />

一切正常。我的问题:是否有可能同时以其他语言获得国家,州,城镇?我想把它放在隐藏的字段中并将其存储在数据库中。我不能将我的api密钥放入代码片段,所以它不会在这里工作,但在家里工作正常。

谢谢,

迈克尔

1 个答案:

答案 0 :(得分:0)

不,您无法一次将API本地化为多种语言。提交后,您可以使用本地化为另一种语言的地理编码服务,在服务器端查找自动填充搜索返回的地点ID,然后存储该地址信息。