0

Я использую следующий код из Googles API в/сценарии входа регистр, чтобы получить адрес пользователя:Google геолокации получить те же данные на разных языках

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> 
 
     \t \t \t <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 в фрагмент кода, поэтому он не будет работать здесь, но дома он работает нормально.

Спасибо,

Майкл

ответ

0

Нет, вы не можете локализовать API для более чем одного языка одновременно. Возможно, после отправки вы сможете использовать службу геокодирования, локализованную на другом языке, на стороне сервера, чтобы найти идентификатор места, возвращаемый поиском автозаполнения, а затем сохранить эту информацию о адресе.

Смежные вопросы