Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- autocompleteOptions = {
- types: ['address'] // 'address' oblige des adresses précises, '(cities)' des villes, etc, voir ici: https://developers.google.com/places/supported_types
- // pour autoriser uniquement des villes et des CP, utiliser ['locality','postal_code','administrative_area_level_3']
- };
- monAutocomplete = new google.maps.places.Autocomplete(document.getElementById('mon-autocomplete'),autocompleteOptions);
- monAutocomplete.addListener('place_changed',function() {
- var place = monAutocomplete.getPlace();
- if (!place.geometry) {
- alert("Erreur ! Sélectionnez une adresse.");
- }
- else {
- var lat = place.geometry.location.lat();
- var lng = place.geometry.location.lng();
- var etablissement = '';
- var numero = '';
- var rue = '';
- var cp = '';
- var ville = place.vicinity;
- var departement = '';
- var departement_code = '';
- var region = '';
- var region_code = '';
- var pays = '';
- var pays_code = '';
- $.each(place.address_components,function(key,address_component) {
- $.each(address_component.types,function(key,type) {
- if (type=='street_number') {
- numero = address_component.long_name;
- }
- else if (type=='route') {
- rue = address_component.long_name;
- }
- else if (type=='locality') {
- ville = address_component.long_name;
- }
- else if (type=='postal_code') {
- cp = address_component.long_name;
- }
- else if (type=='administrative_area_level_2') {
- departement = address_component.long_name;
- departement_code = address_component.short_name;
- }
- else if (type=='administrative_area_level_1') {
- region = address_component.long_name;
- region_code = address_component.short_name;
- }
- else if (type=='country') {
- pays = address_component.long_name;
- pays_code = address_component.short_name;
- }
- });
- });
- if (departement_code && departement_code==departement && cp)
- departement_code = (''+cp).substr(0,2);
- if (place.types) {
- $.each(place.types,function(i,type) {
- if (type=='establishment')
- etablissement = place.name;
- });
- }
- // ICI FAIRE CE QU'ON VEUT DES DONNÉES
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement