I have a Node URL (created using Express) that can be used to download a
static image of an address. So, the calling app calls the /download url and
passes multiple addresses using json, then the download service would call
the Google Maps service and save the static image of each of these addreses
on the node server (it would then send it back to the calling application,
but for the purpose of this question I am only interested in saving the
images in the node server).
Initially, we were only interested in saving satellite view of the
addresses, so I wrote this code
var request = require('request');
function saveMap(addressJson) {
return Promise.all(
//iterate over address json, get each address, call the Google Maps
URL and save it.
addressJson.map(function(item) {
return new Promise(function (resolve, reject) {
var mapUrl =
'http://maps.googleapis.com/maps/api/staticmap?maptype=roadmap&markers=size:mid|color:red|'
+ item.address;
request(mapUrl)
.pipe(fs.createWriteStream('/temp/' + item.id + '.jpg'
))
.on('finish', function () {
resolve("Promise resolved");
}).on('error', function (error) {
reject('Error in creating map', error);
})
});
})
)
}
The Promises for saving each address is wrapped inside Promise.all(..),
since I want to the saveMap() to return when all maps have finished
downloading (so that I can zip them and send to the calling app, so need to
be sure that everything has been downloaded).
Now, we need to extend this function to also include satellite maps. so, I
was hoping, that within same json iteration, I can have another Promise
which would download satellite maps. Something like this
function saveMap(addressJson) {
return Promise.all(
//iterate over address json, get each address, call the Google Maps
URL and save it.
addressJson.map(function(item) {
return new Promise(function (resolve, reject) {
var mapUrl =
'http://maps.googleapis.com/maps/api/staticmap?maptype=roadmap|' + item.
address;
request(mapUrl)
.pipe(fs.createWriteStream('/temp/r/' + item.id +
'.jpg'))
.on('finish', function () {
resolve("Promise resolved");
}).on('error', function (error) {
reject('Error in creating map', error);
})
});
return new Promise(function (resolve, reject) {
var mapUrl2 =
'http://maps.googleapis.com/maps/api/staticmap?maptype=satellite|' + item.
address;
req(mapUrl2)
.pipe(fs.createWriteStream(fs.createWriteStream(
'/temp/s/' + item.id + '.jpg'))
.on('finish', function () {
resolve("Promised resolved");
}).on('error', function (error) {
reject('Error in creating map', error);
})
});
})
)
}
However, this does not work as expected. I don't get an error but it is not
generating all the jpg files. Can someone please help me understand what I
am doing wrong and how to get this corrected.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/31ba0068-50d2-4a31-8559-a4b1c3c7a45b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.