1

I am writing a small script that takes an array of locations and geocodes them to add lat long to another object. I have it working so it geocodes and writes to the console flawlessly. How can I take that output and put it into a predefined variable outside of the function? I am not vary familiar with jquery and do not intend on using it anyplace else in the script.

Just started learning JavaScript this weekend so don't shoot me if Something else is messed up.

Thanks for any help!!

Also, I changed my Google API key so it isn't stolen.

      //test array of locations to geocode
var locationsToGeoCode = ["China: Shandong: Jinan",
    "China: Shandong: Jinan",
    "United States: Washington: La Conner",
    "United States: Texas: Dallas",
    "United States: California: Walnut"
    ];

//empty object to place geocoded places
var coordinates;

for (var i = 0; i < locationsToGeoCode.length; i += 1) {
  $.getJSON("https://maps.googleapis.com/maps/api/geocode/json?address=" + locationsToGeoCode[i] + "&key=MIzaSyCywKsD_50EI9rheDLyqPOUdUzi6s6u-q8", function (geocodeResult) {
        console.log(geocodeResult);
        });
    }
4
  • 1
    coordinates.push(geocodeResult)? Commented Apr 13, 2015 at 4:26
  • @BradChristie that would work if he wanted an array, but from the question I think he wanted an object Commented Apr 13, 2015 at 4:30
  • I guess I wasn't taking "empty object" as literally as I should have been. ;-) Commented Apr 13, 2015 at 4:31
  • I don't think there's a clear preference expressed for array vs. object. However, an array may be problematic when you have multiple async requests in-flight concurrently, with an object it's easier to avoid those kind sof problems by keying the responses properly. Commented Apr 13, 2015 at 4:34

1 Answer 1

1

Modifying your code like such should work:

      //test array of locations to geocode
var locationsToGeoCode = ["China: Shandong: Jinan",
    "China: Shandong: Jinan",
    "United States: Washington: La Conner",
    "United States: Texas: Dallas",
    "United States: California: Walnut"
    ];

//empty object to place geocoded places
var coordinates = {};

function fetchGeoCode(location){
     $.getJSON("https://maps.googleapis.com/maps/api/geocode/json?address=" + location + "&key=MIzaSyCywKsD_50EI9rheDLyqPOUdUzi6s6u-q8", function (geocodeResult) {
        coordinates[location] = geocodeResult;
    });
}

for (var i = 0; i < locationsToGeoCode.length; i += 1) {
    fetchGeoCode(locationsToGeoCode[i]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 as this also averts potential issues with firing multiple async requests in rapid succession, such as having results returned in an arbitrary order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.