Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
125 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

function geocodeAddress(address, i) {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       var latLong = results[0].geometry.location;
       coordinates = latLong.lat() + "," + latLong.lng();
       adressdaten[i][6] = coordinates;
    } else {
       alert('Geocode of '+address+' was not successful for the following reason: ' + status);
    }
  });
}

var geocoder = new google.maps.Geocoder();
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;
  geocodeAddress(addresse, i); 

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...