Permalink
Browse files

Merge branch 'release/0.1.5'

  • Loading branch information...
moshen committed Dec 6, 2011
2 parents eef5b88 + d9c0008 commit 70b0adbcc41998d5b2528ac332b07a47eb379285
Showing with 187 additions and 10 deletions.
  1. +43 −4 README.md
  2. +75 −2 lib/googlemaps.js
  3. +2 −1 package.json
  4. +7 −3 test/reverseGeocode-test.js
  5. +60 −0 test/staticmaps-test.js
View
@@ -21,10 +21,10 @@ APIs implemented:
* [Places](http://code.google.com/apis/maps/documentation/places/)
* [Place Details](https://code.google.com/apis/maps/documentation/places/#PlaceDetails)
* [Distance Matrix](http://code.google.com/apis/maps/documentation/distancematrix/)
+* [Static Maps](http://code.google.com/apis/maps/documentation/staticmaps/)
TODO:
-* [Static Maps](http://code.google.com/apis/maps/documentation/staticmaps/)
* [Tests for everything](http://github.com/moshen/node-googlemaps/tree/master/test/) (using [vows](http://vowsjs.org/))
# Usage
@@ -41,6 +41,44 @@ TODO:
Both examples print:
{"status":"OK","results":[{"types":["postal_code"],"formatted_address":"Chicago, IL 60695, USA"...
+
+For the Static Maps API, you can pass in all the required parameters as well as markers, styles, and paths using the formats outlined below.
+
+ markers = [
+ { 'location': '300 W Main St Lock Haven, PA' },
+ { 'location': '444 W Main St Lock Haven, PA',
+ 'color': 'red',
+ 'label': 'A',
+ 'shadow': 'false',
+ 'icon' : 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe%7C996600'
+ }
+ ]
+
+ styles = [
+ { 'feature': 'road', 'element': 'all', 'rules':
+ { 'hue': '0x00ff00' }
+ }
+ ]
+
+ paths = [
+ { 'color': '0x0000ff', 'weight': '5', 'points':
+ [ '41.139817,-77.454439', '41.138621,-77.451596' ]
+ }
+ ]
+
+ sys.puts(gm.staticMap('444 W Main St Lock Haven PA', 15, '500x400', false, false, 'roadmap', markers, styles, paths));
+
+This example prints the URL for the Static Map image: "http://maps.googleapis.com/maps/api/staticmap?center=444%20W%20Main%20St%20Lock%20Haven%20PA&zoom=15&size=500x400&maptype=roadmap&markers=%7C300%20W%20Main%20St%20Lock%20Haven%2C%20PA&markers=%7Ccolor%3Ared%7Clabel%3AA%7Cicon%3Ahttp%3A%2F%2Fchart.apis.google.com%2Fchart%3Fchst%3Dd_map_pin_icon%26chld%3Dcafe%257C996600%7Cshadow%3Afalse%7C444%20W%20Main%20St%20Lock%20Haven%2C%20PA&style=%7Cfeature%3Aroad%7Celement%3Aall%7Chue%3A0x00ff00&path=weight%3A5%7Ccolor%3A0x0000ff%7C41.139817%2C-77.454439%7C41.138621%2C-77.451596&sensor=false"
+
+By giving gm.staticMap an optional callback, you can retreive the static map PNG data:
+
+ sys.puts(gm.staticMap('444 W Main St Lock Haven PA', 15, '500x400', function(err, data){
+ require('fs').writeFileSync('test_map.png', data, 'binary');
+ }, false, 'roadmap', markers, styles, paths));
+
+You will get a map like:
+
+![Some Map](http://maps.googleapis.com/maps/api/staticmap?center=444%20W%20Main%20St%20Lock%20Haven%20PA&zoom=15&size=500x400&maptype=roadmap&markers=%7C300%20W%20Main%20St%20Lock%20Haven%2C%20PA&markers=%7Ccolor%3Ared%7Clabel%3AA%7Cicon%3Ahttp%3A%2F%2Fchart.apis.google.com%2Fchart%3Fchst%3Dd_map_pin_icon%26chld%3Dcafe%257C996600%7Cshadow%3Afalse%7C444%20W%20Main%20St%20Lock%20Haven%2C%20PA&style=%7Cfeature%3Aroad%7Celement%3Aall%7Chue%3A0x00ff00&path=weight%3A5%7Ccolor%3A0x0000ff%7C41.139817%2C-77.454439%7C41.138621%2C-77.451596&sensor=false)
All the googlemaps functions follow this scheme:
function(required, callback, optional)
@@ -53,7 +91,8 @@ Please refer to the code, [tests](http://github.com/moshen/node-googlemaps/tree/
# Contributors
-* [evnm](https://github.com/evnm)
-* [duncanm](https://github.com/duncanm)
-* [sugendran](https://github.com/sugendran)
+[![evnm](https://secure.gravatar.com/avatar/2a8171b6c385b865e30bf070cf588329?s=50)](https://github.com/evnm)
+[![duncanm](https://secure.gravatar.com/avatar/7310945bafb21aa68b18d61d8b9d2d61?s=50)](https://github.com/duncanm)
+[![sugendran](https://secure.gravatar.com/avatar/3228aae57c1dc3f657bbc64c26c97b77?s=50)](https://github.com/sugendran)
+[![JoshSmith](https://secure.gravatar.com/avatar/b07d5a5f2e75633b2085142250a6762b?s=50)](https://github.com/JoshSmith)
View
@@ -117,6 +117,77 @@ exports.elevationFromPath = function(path , samples , callback , sensor){
makeRequest(reqPath, false, returnObjectFromJSON(callback));
}
+// http://code.google.com/apis/maps/documentation/staticmaps
+exports.staticMap = function(center , zoom , size , callback , sensor ,
+ maptype , markers, styles, paths){
+ var args = {
+ 'center': center,
+ 'zoom': zoom,
+ 'size': size
+ };
+ var i , k;
+
+ if(maptype){ args.maptype = maptype; }
+ if(markers){
+ args.markers = [];
+ for(i=0; i < markers.length; i++) {
+ var marker = '';
+ if(markers[i].size) { marker += '|size:' + markers[i].size; }
+ if(markers[i].color) { marker += '|color:' + markers[i].color; }
+ if(markers[i].label) { marker += '|label:' + markers[i].label; }
+ if(markers[i].icon) { marker += '|icon:' + markers[i].icon; }
+ if(markers[i].shadow) { marker += '|shadow:' + markers[i].shadow; }
+ if(markers[i].location){ marker += '|' + markers[i].location; }
+ args.markers[i] = marker;
+ }
+ }
+ if(styles){
+ args.style = [];
+ for(i=0; i < styles.length; i++) {
+ var new_style = '';
+ if(styles[i].feature){ new_style += '|feature:' + styles[i].feature; }
+ if(styles[i].element){ new_style += '|element:' + styles[i].element; }
+
+ var rules = styles[i].rules;
+
+ if(rules){
+ for(k in rules) {
+ var rule = rules[k];
+ new_style += '|' + k + ':' + rule;
+ }
+ }
+ args.style[i] = new_style;
+ }
+ }
+ if(paths){
+ args.path = [];
+ for(i=0; i < paths.length; i++) {
+ var new_path = '';
+ if(paths[i].weight) { new_path += '|weight:' + paths[i].weight; }
+ if(paths[i].color) { new_path += '|color:' + paths[i].color; }
+ if(paths[i].fillcolor){ new_path += '|fillcolor:' + paths[i].fillcolor; }
+
+ var points = paths[i].points;
+
+ if(points){
+ for(k=0; k < points.length; k++) {
+ new_path += '|' + points[k];
+ }
+ }
+ args.path[i] = new_path.replace(/^\|/, '');
+ }
+ }
+ args.sensor = sensor || 'false';
+
+ var path = '/maps/api/staticmap?' + qs.stringify(args);
+
+ if( typeof( callback ) === 'function' ){
+ makeRequest( path , false , callback , 'binary' );
+ }
+
+ return 'http://maps.googleapis.com' + path;
+}
+
// Helper function to check and convert an array of points, be it strings/numbers/etc
// into the format used by Google Maps for representing lists of latitude/longitude pairs
exports.checkAndConvertArrayOfPoints = function(input){
@@ -160,11 +231,14 @@ var returnObjectFromJSON = function(callback){
// Makes the request to Google Maps API.
// If secure is true, uses https. Otherwise http is used.
-var makeRequest = function(path, secure, callback){
+var makeRequest = function(path, secure, callback, encoding){
var protocol = (secure) ? require('https') : require('http')
, options = { host: 'maps.googleapis.com', path: path, }
protocol.get(options, function (res) {
+ if( encoding ){
+ res.setEncoding(encoding);
+ }
var data = "";
var httpEnd = function(){
if (res.statusCode == 200) callback(null, data);
@@ -177,4 +251,3 @@ var makeRequest = function(path, secure, callback){
res.on('close', httpEnd);
}).on('error', function (err) { callback(err); });
}
-
View
@@ -1,8 +1,9 @@
{
"name": "googlemaps",
- "version": "0.1.4",
+ "version": "0.1.5",
"main": "./lib/googlemaps",
"description": "A simple way to query the Google Maps API from Node.js",
+ "keywords": ["map", "geo", "api"],
"repository": {
"type": "git",
"url": "http://github.com/moshen/node-googlemaps"
@@ -10,8 +10,12 @@ vows.describe('reverseGeocode').addBatch({
'returns as a valid request': function(err, result){
assert.equal(result.status , 'OK');
},
- 'returns expected name (Chicago)': function(err, result){
- assert.equal(result.results[0].address_components[1].long_name , 'Chicago')
+ // For some reason the location of "Chicago" is constantly changing
+ // according to Google. I thought that it would be a constant I could
+ // rely on for these tests. If I have to change it one more time,
+ // I'm going to just comment them all out.
+ 'returns expected name (Pilsen)': function(err, result){
+ assert.equal(result.results[0].address_components[1].long_name , 'Pilsen')
}
}
}).export(module);
@@ -1008,4 +1012,4 @@ vows.describe('reverseGeocode').addBatch({
}
]
}
-*/
+*/
View
@@ -0,0 +1,60 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ crypto = require('crypto'),
+ gm = require('../lib/googlemaps');
+
+vows.describe('staticmaps').addBatch({
+ 'Complex static map (Lock Haven, PA)': {
+ topic: {
+ 'markers': [
+ { 'location': '300 W Main St Lock Haven, PA' },
+ { 'location': '444 W Main St Lock Haven, PA',
+ 'color' : 'red',
+ 'label' : 'A',
+ 'shadow' : 'false',
+ 'icon' : 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe%7C996600'
+ }
+ ],
+
+ 'styles': [
+ { 'feature': 'road', 'element': 'all', 'rules':
+ { 'hue': '0x00ff00' }
+ }
+ ],
+
+ 'paths': [
+ { 'color': '0x0000ff', 'weight': '5', 'points':
+ [ '41.139817,-77.454439', '41.138621,-77.451596' ]
+ }
+ ]
+ },
+
+ 'URL': {
+ topic: function(options){
+ return gm.staticMap('444 W Main St Lock Haven PA', 15, '500x400',
+ false, false, 'roadmap', options.markers, options.styles, options.paths);
+ },
+ 'returns the expected static map URL': function(result){
+ assert.equal(result , "http://maps.googleapis.com/maps/api/staticmap?center=444%20W%20Main%20St%20" +
+ "Lock%20Haven%20PA&zoom=15&size=500x400&maptype=roadmap&markers=%7C300%20W%2" +
+ "0Main%20St%20Lock%20Haven%2C%20PA&markers=%7Ccolor%3Ared%7Clabel%3AA%7Cicon" +
+ "%3Ahttp%3A%2F%2Fchart.apis.google.com%2Fchart%3Fchst%3Dd_map_pin_icon%26chl" +
+ "d%3Dcafe%257C996600%7Cshadow%3Afalse%7C444%20W%20Main%20St%20Lock%20Haven%2" +
+ "C%20PA&style=%7Cfeature%3Aroad%7Celement%3Aall%7Chue%3A0x00ff00&path=weight" +
+ "%3A5%7Ccolor%3A0x0000ff%7C41.139817%2C-77.454439%7C41.138621%2C-77.451596&s" +
+ "ensor=false");
+ }
+ },
+
+ 'PNG data': {
+ topic: function(options){
+ gm.staticMap('444 W Main St Lock Haven PA', 15, '500x400', this.callback, false, 'roadmap', options.markers, options.styles, options.paths);
+ },
+ 'returns the expected static map PNG data': function(err, data){
+ var md5 = crypto.createHash('md5');
+ md5.update(data);
+ assert.equal(md5.digest('hex') , 'e1c4533cd8f676f2aef58d7a28d9f773');
+ }
+ }
+ }
+}).export(module);

0 comments on commit 70b0adb

Please sign in to comment.