I want to calculate the address backwards from latitude and longitude in Google Map API and process it repeatedly, but only one comes out.

Asked 2 months ago, Updated 2 months ago, 2 views

The Google Map API calculates the address backwards from latitude and longitude.
I can do one without any problems, but I want to process a large amount of data in bulk, so
Attempting to repeat in array.
However, even if I write in the code below, only the last data will be displayed.
What's wrong?
JS has no experience, so I collected advice on the web and wrote it down.
I'm sorry if you're a beginner and ask silly questions...

<script type="text/javascript">

// find one's address by latitude and longitude
function getAdrs(){

    var markers = [
        ["Kochi Prefectural Museum of Art", 35.663778, 139.73951]
        ["Makino Botanical Garden", 33.54661, 133.57790]
        ["Kochi Castle", 33.56067, 133.53147]
        ["Katsurahama", 33.497145, 133.57480]
        ["Kochi City Culture Plaza Carport", 33.55826, 133.54725]

    for (vari=0;i<markers.length;i++) {
        var name = markers[i][0];

        var latlng = new google.maps.LatLng (markers[i][1], markers[i][2])
        vargc = new google.maps.Geocoder();

        function(results, status){
                startxt = name;
                for (vari=0;i<adrsData.length;i++){
                    txt+=adrsData[i].long_name+", ";
                    // txt+=adrsData[i].types+"<br>";
            } else {
                alert(status+": geocode failed";


javascript api google-maps

2022-09-30 12:00

2 Answers

This Code


If is the output location, then only the final result will be output because the result is overwritten.


Use or


Wouldn't it be better to do something like that?

2022-09-30 12:00

The callback occurs after the loop ends, so the variable does not appear to be the expected value.
function(results, status){...} part

(function(n){return function(results, status){
    var name = markers[n][0];
}; })(i);

You must rewrite name as shown in .

2022-09-30 12:00

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.