I'm trying to retrieve the address that API google send me into an array. The problem is that I just received
an array empty(Address) with all my elements.
I tried to async: false my ajax because of my first impression
was that the asynchronous was the problem but it's done nothing.
If you have any idea it will be good, thanks.
Output :
(4) [Array(0), Array(0), Array(0), Array(0)]
0: Array(0)
id: 9
coord: "Rue des Haies 56, 6001 Charleroi, Belgique"
length: 0
__proto__: Array(0)
1: [id: 10, coord: "43 Rue de Boulainvilliers, 75016 Paris, France"]
2: [id: 11, coord: "Grand Place 22, 7000 Mons, Belgique"]
3: [id: 12, coord: "28 Place Sébastopol, 59000 Lille, France"]
length: 4
__proto__: Array(0)
My code:
let geocoder = new google.maps.Geocoder;
$.ajax({
type: "POST",
url: "{{ path('url') }}",
async : false,
success: function (data) {
let positions = JSON.parse(data);
let allAddress = [];
Array.from(positions).map((position, index) => {
allAddress[index] = [];
let latlng = {
lat: parseFloat(position['latitude']),
lng: parseFloat(position['longitude'])
};
let idPosition = position['id'];
geocoder.geocode({'location': latlng}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
let searchCoords = results[0]['formatted_address'];
setTimeout(function() {
allAddress[index]['id'] = idPosition;
allAddress[index]['coord'] = searchCoords;
}, 0);
} else {
console.log("Geocode wasn't successful for the following reason : " + status);
}
});
});
console.log(allAddress);
}
});