Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

javascript - Saving geocoder results to an array - Closure Trouble

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

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

1 Reply

0 votes
by (71.8m points)

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); 

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...