Skip to content

Instantly share code, notes, and snippets.

@yhsiang
Last active December 19, 2015 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhsiang/6019574 to your computer and use it in GitHub Desktop.
Save yhsiang/6019574 to your computer and use it in GitHub Desktop.
var http = require('httpsync')
, fs = require('fs');
var getLocation = function (address, callback) {
var url, req, res, results, location;
url = 'http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&sensor=false';
req = http.get(url);
res = req.end();
if(JSON.parse(res.data).status === 'OK') {
results = JSON.parse(res.data).results[0];
location = results.geometry.location;
callback(location);
} else if (JSON.parse(res.data).status === 'OVER_QUERY_LIMIT'){
//setTimeout( function(){ getLocation(address, callback);}, 3000);
}
};
var json =fs.readFileSync('public/data/farms.json');
var farms = JSON.parse(json.toString()).farms;
farms.forEach(function (d, i) {
if(d.address.match(/號/) && i < 200 ) {
getLocation(d.address.replace(/ /g,''), function(location) {
d.location = location;
console.log(JSON.stringify(d)+',');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment