Created
March 7, 2016 19:42
-
-
Save wesww/cb3f7dc1340394549e81 to your computer and use it in GitHub Desktop.
batch update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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