Skip to content

Instantly share code, notes, and snippets.

@wesww
Created March 7, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesww/cb3f7dc1340394549e81 to your computer and use it in GitHub Desktop.
Save wesww/cb3f7dc1340394549e81 to your computer and use it in GitHub Desktop.
batch update
function geocodeItem(item) {
var itemId = item.ID;
var city = item.venue_address_city;
city = city.replace(' ', '+').replace(',', '');
if (item.lat == '0.000000' || item.lng == "0.000000") {
jQuery.ajax({
url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + city,
method: "GET"
}).done(function(response) {
var latlng = response.results[0].geometry.location;
var postData = {
table: "wp_mf_global_faire",
type: "updateData",
"data[ID]": itemId,
pKeyField: "ID",
"data[lat]": latlng.lat,
"data[lng]": latlng.lng
};
// must be run from resources page console while logged in
// otherwise requests will bounce without proper cookie
jQuery.ajax({
url: "http://makerfaire.com/resource-mgmt/ajax.php",
method: "POST",
data: postData
});
});
}
}
// Delayed loop. Regular loop failed because of Google api request limits
(function apiDelayLoop(i) {
setTimeout(function() {
geocodeItem(data[i - 1]);
if (--i) {
apiDelayLoop(i);
}
}, 1000)
})(data.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment