Okay, so I have searched a while for a solution to this problem, but I have found nothing specifically on this. And before you point me to Google's Terms of Service, please read to the ende of the question!
So here's the idea: I want to save the Latitude and Longitude of an address into an array using Google's Geocoder. I managed to calculate all the values correctly, but I just can't seem to save it to the array. I have already used an anonymous function to pass the address to the function, but the saving is still not working. Please help!
Regarding Google's Terms of Service: I know that I may not save this code anywhere and not display it in a Google Map. But I need to save it as a kml-file to feed into a Google Map later. I know, it would be much more convenient to just create the Map, but for other reasons that's not possible.
adressdaten[] is a two-dimensional array with the data of the addresses This is the code:
for (i=1; i<adressdaten.length-1; i++) {
//Save array-data in String to pass to the Geocoder
var adresse = adressdaten[i][3] + " " + adressdaten[i][4];
var coordinates;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': adresse}, (function (coordinates, adresse) {
return function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLong = results[0].geometry.location;
coordinates = latLong.lat() + "," + latLong.lng();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
}
})(coordinates, adresse));
adressdaten[i][6] = coordinates;
}
This is a FAQ. Geocoding is asynchronous. You need to save the results in the callback function which runs when they are returned from the server.
Something like (not tested)
Updated to use function closure
}