0

J'utilise le code suivant de l'API Googles dans un script Inscription/login, pour obtenir l'adresse de l'utilisateur:Google géolocalisation obtenir les mêmes données dans différentes langues

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" />

Tout fonctionne très bien . Ma question: Y at-il une possibilité d'obtenir le pays, l'état, la ville dans d'autres langues en même temps? Je veux le mettre dans des champs cachés et le stocker dans la base de données. Je ne peux pas mettre ma clé api dans l'extrait de code, donc ça ne marchera pas ici, mais à la maison cela fonctionne bien.

Merci,

Michael

Répondre

0

Non, vous ne pouvez pas localiser l'API pour plus d'une langue à la fois. Vous pourrez peut-être, une fois que vous soumettez, utiliser le service de géocodage, localisé dans l'autre langue, côté serveur pour rechercher l'ID de lieu renvoyé par la recherche de saisie semi-automatique, puis stocker cette information d'adresse.