├── .gitignore ├── .jshintrc ├── .travis.yml ├── CHANGELOG.md ├── Gruntfile.js ├── README.md ├── angular-google-staticmaps.js ├── angular-google-staticmaps.min.js ├── bower.json ├── index.html ├── karma.conf.js ├── package.json ├── src └── angular-google-staticmaps.js └── test └── spec └── directive.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components/ 2 | *.log 3 | node_modules/ 4 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "indent": 2, 4 | "esnext": true, 5 | "bitwise": false, 6 | "curly": false, 7 | "eqeqeq": true, 8 | "eqnull": true, 9 | "immed": true, 10 | "latedef": true, 11 | "newcap": true, 12 | "noarg": true, 13 | "undef": true, 14 | "strict": false, 15 | "trailing": true, 16 | "smarttabs": true, 17 | "white": true, 18 | "jquery": true, 19 | "globals": { 20 | "angular": false, 21 | "sinon": false 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.8' 4 | - '0.10' 5 | before_script: 6 | - 'npm install -g bower grunt-cli' 7 | - 'bower install' 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ## v0.3.0 (2014-11-23) 3 | 4 | 5 | #### Features 6 | 7 | * **deps:** Upgrade to Angular 1.3 ([d7a6dee0](http://github.com/passy/angular-google-staticmaps/commit/d7a6dee05d6752797c8311a99a44c571981d9d11)) 8 | 9 | 10 | ## v0.2.0 (2013-11-10) 11 | 12 | 13 | #### Features 14 | 15 | * **deps:** Upgrade to Angular 1.2 ([b808bae5](http://github.com/passy/angular-google-staticmaps/commit/b808bae51fd70d061318cb60f7e6457179394daf)) 16 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function (grunt) { 2 | 'use strict'; 3 | 4 | grunt.initConfig({ 5 | pkg: grunt.file.readJSON('bower.json'), 6 | uglify: { 7 | options: { 8 | preserveComments: 'some' 9 | }, 10 | dist: { 11 | src: '<%= pkg.name %>.js', 12 | dest: '<%= pkg.name %>.min.js' 13 | } 14 | }, 15 | concat: { 16 | options: { 17 | process: true 18 | }, 19 | dist: { 20 | src: 'src/<%= pkg.name %>.js', 21 | dest: '<%= pkg.name %>.js' 22 | } 23 | }, 24 | karma: { 25 | dist: { 26 | configFile: 'karma.conf.js', 27 | browsers: ['PhantomJS'] 28 | }, 29 | watch: { 30 | configFile: 'karma.conf.js', 31 | singleRun: false, 32 | autoWatch: true 33 | } 34 | }, 35 | changelog: { 36 | options: { 37 | github: 'passy/angular-google-staticmaps' 38 | } 39 | } 40 | }); 41 | 42 | grunt.loadNpmTasks('grunt-contrib-uglify'); 43 | grunt.loadNpmTasks('grunt-contrib-concat'); 44 | grunt.loadNpmTasks('grunt-karma'); 45 | grunt.loadNpmTasks('grunt-conventional-changelog'); 46 | grunt.registerTask('default', ['concat', 'uglify']); 47 | grunt.registerTask('test', ['karma:dist']); 48 | }; 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Google Static Maps Directive [![Build Status](https://travis-ci.org/passy/angular-google-staticmaps.png)](https://travis-ci.org/passy/angular-google-staticmaps) [![Dependency Status](https://gemnasium.com/passy/angular-google-staticmaps.png)](https://gemnasium.com/passy/angular-google-staticmaps) 2 | 3 | [Homepage](http://passy.github.io/angular-google-staticmaps) 4 | 5 | An [AngularJS](http://angularjs.org/) directive to quickly insert [Static 6 | Maps](https://developers.google.com/maps/documentation/staticmaps/). 7 | 8 | ## Usage 9 | 10 | 1. `bower install --save angular-google-staticmaps` 11 | 2. Include dependencies in your HTML. 12 | 3. Load the `wu.staticGmap` module for your Angular app. 13 | 4. Use the `static-gmap` directive. 14 | 15 | ## Example 16 | 17 | See the [homepage](http://passy.github.io/angular-google-staticmaps) for a live example. 18 | 19 | ```html 20 | 21 | ``` 22 | 23 | The `markers` attribute is an expression evaluating to either one or multiple 24 | markers. Markers have the following format: 25 | 26 | ```javascript 27 | $scope.markers = [{ 28 | color: 'blue', 29 | label: 'S', 30 | coords: [lat, lon] 31 | }]; 32 | ``` 33 | 34 | ## Attributes 35 | 36 | Any attribute is directly passed to the generated URL the image is loaded from, 37 | except for `markers`, which gets formatted according to the specification. 38 | 39 | ### `size` (required) 40 | 41 | The size attribute is required and must be specified as `wxh` whereby `w` 42 | denotes the width and pixels and `h` the height. 43 | 44 | ### `sensor` (required) 45 | 46 | The sensor attribute must explicitly be set to either `true` or `false`. 47 | 48 | ## Contributing 49 | 50 | Pull requests welcome. Only change files in `src` and don't bump any versions. 51 | Please respect the code style in place. 52 | 53 | ## License 54 | 55 | MIT 56 | -------------------------------------------------------------------------------- /angular-google-staticmaps.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * angular-google-staticmaps 0.0.1 3 | * Pascal Hartig, weluse GmbH, http://weluse.de/ 4 | * License: MIT 5 | */ 6 | (function () { 7 | 'use strict'; 8 | 9 | angular.module('wu.staticGmap', []) 10 | .controller('StaticGmapCtrl', function () { 11 | var BASE_URL = '//maps.googleapis.com/maps/api/staticmap?'; 12 | var STYLE_ATTRIBUTES = ['color', 'label', 'size']; 13 | 14 | this.makeMarkerStrings = function makeMarkerStrings(markers) { 15 | return markers.map(function (marker) { 16 | var str = Object.keys(marker).map(function (key) { 17 | if (STYLE_ATTRIBUTES.indexOf(key) > -1) { 18 | return key + ':' + marker[key] + '|'; 19 | } 20 | }).join(''); 21 | 22 | return str + marker.coords.join(','); 23 | }); 24 | }; 25 | 26 | this.buildSourceString = function buildSourceString(attrs, markers) { 27 | var markerStrings; 28 | 29 | if (markers) { 30 | if (!angular.isArray(markers)) { 31 | markers = [markers]; 32 | } 33 | markerStrings = this.makeMarkerStrings(markers); 34 | } 35 | 36 | var params = Object.keys(attrs).map(function (attr) { 37 | if (attr === 'markers' && markerStrings) { 38 | return Object.keys(markerStrings).map(function (key) { 39 | return 'markers=' + encodeURIComponent(markerStrings[key]); 40 | }).join('&'); 41 | } 42 | 43 | if (attr[0] !== '$' && attr !== 'alt') { 44 | return encodeURIComponent(attr) + '=' + encodeURIComponent(attrs[attr]); 45 | } 46 | }); 47 | 48 | return BASE_URL + params.reduce(function (a, b) { 49 | if (!a) { 50 | return b; 51 | } 52 | 53 | if (b !== undefined) { 54 | return a + '&' + b; 55 | } 56 | 57 | return a; 58 | }, ''); 59 | }; 60 | }) 61 | .directive('staticGmap', function ($parse) { 62 | return { 63 | template: 'Google Map', 64 | replace: true, 65 | restrict: 'E', 66 | controller: 'StaticGmapCtrl', 67 | scope: true, 68 | 69 | link: function postLink(scope, element, attrs, ctrl) { 70 | var el = element[0]; 71 | var markers = $parse(attrs.markers)(scope); 72 | 73 | if (!attrs.sensor) { 74 | throw new Error('The `sensor` attribute is required.'); 75 | } 76 | 77 | if (!attrs.size) { 78 | throw new Error('The `size` attribute is required.'); 79 | } 80 | 81 | var sizeBits = attrs.size.split('x'); 82 | if (sizeBits.length !== 2) { 83 | throw new Error('Size must be specified as `wxh`.'); 84 | } 85 | 86 | el.width = parseInt(sizeBits[0], 10); 87 | el.height = parseInt(sizeBits[1], 10); 88 | el.src = ctrl.buildSourceString(attrs, markers); 89 | } 90 | }; 91 | }); 92 | }()); 93 | -------------------------------------------------------------------------------- /angular-google-staticmaps.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * angular-google-staticmaps 0.0.1 3 | * Pascal Hartig, weluse GmbH, http://weluse.de/ 4 | * License: MIT 5 | */ 6 | !function(){"use strict";angular.module("wu.staticGmap",[]).controller("StaticGmapCtrl",function(){var a="//maps.googleapis.com/maps/api/staticmap?",b=["color","label","size"];this.makeMarkerStrings=function(a){return a.map(function(a){var c=Object.keys(a).map(function(c){return b.indexOf(c)>-1?c+":"+a[c]+"|":void 0}).join("");return c+a.coords.join(",")})},this.buildSourceString=function(b,c){var d;c&&(angular.isArray(c)||(c=[c]),d=this.makeMarkerStrings(c));var e=Object.keys(b).map(function(a){return"markers"===a&&d?Object.keys(d).map(function(a){return"markers="+encodeURIComponent(d[a])}).join("&"):"$"!==a[0]&&"alt"!==a?encodeURIComponent(a)+"="+encodeURIComponent(b[a]):void 0});return a+e.reduce(function(a,b){return a?void 0!==b?a+"&"+b:a:b},"")}}).directive("staticGmap",function(a){return{template:'Google Map',replace:!0,restrict:"E",controller:"StaticGmapCtrl",scope:!0,link:function(b,c,d,e){var f=c[0],g=a(d.markers)(b);if(!d.sensor)throw new Error("The `sensor` attribute is required.");if(!d.size)throw new Error("The `size` attribute is required.");var h=d.size.split("x");if(2!==h.length)throw new Error("Size must be specified as `wxh`.");f.width=parseInt(h[0],10),f.height=parseInt(h[1],10),f.src=e.buildSourceString(d,g)}}})}(); -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-google-staticmaps", 3 | "description": "An AngularJS directive for Google Static Maps V2.", 4 | "version": "0.3.0", 5 | "main": "./angular-google-staticmaps.js", 6 | "ignore": [ 7 | "**/.*", 8 | "node_modules", 9 | "bower_components", 10 | "test", 11 | "src", 12 | "*.min.js", 13 | "*.conf.js", 14 | "*.html", 15 | "Gruntfile.js", 16 | "package.json" 17 | ], 18 | "dependencies": { 19 | "angular": "~1.3.0" 20 | }, 21 | "devDependencies": { 22 | "angular-mocks": "~1.2.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AngularJS Google Static Maps Directive 6 | 7 | 12 | 13 | 14 |
15 |

angular-google-staticmaps

16 | 17 |

A directive to embed Google Static Maps in AngularJS through a simple tag.

18 | 19 | 22 | 23 | 26 | 27 | 30 |
31 |
32 |
33 |

Getting Started

34 |
    35 |
  1. Install with bower: bower install --save angular-google-staticmaps
  2. 36 |
  3. Include angular-google-staticmaps.js.
  4. 37 |
  5. Include wu.staticGmap in your module dependencies.
  6. 38 |
  7. Use the static-gmap directive.
  8. 39 |
40 | 41 | If you hate proper package management, you can also 42 | directly download 43 | angular-google-staticmaps.js or 44 | angular-staticmaps.min.js. 45 |
46 |
47 |
48 |
49 |
50 |

In Action

51 | 56 | 57 | 58 |

Code

59 |
<static-gmap
60 |     size="640x200"
61 |     sensor="false"
62 |     zoom="14"
63 |     markers="{ color: 'red', label: 'X', coords: [61.133789, 10.401121] }">
64 | </static-gmap>
65 |
66 |
67 | 68 | Fork me on GitHub 69 | 70 | 71 | 72 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration 2 | module.exports = function (config) { 3 | 'use strict'; 4 | 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine'], 8 | files: [ 9 | 'bower_components/angular/angular.js', 10 | 'bower_components/angular-mocks/angular-mocks.js', 11 | 'src/angular-google-staticmaps.js', 12 | 'test/spec/**/*.coffee' 13 | ], 14 | exclude: [], 15 | reporters: ['dots'], 16 | autoWatch: false, 17 | browsers: ['Chrome'], 18 | captureTimeout: 5000, 19 | singleRun: true, 20 | reportSlowerThan: 100 21 | }); 22 | }; 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-google-staticmaps", 3 | "version": "0.3.0", 4 | "devDependencies": { 5 | "grunt-contrib-uglify": "~0.2.2", 6 | "grunt": "~0.4.1", 7 | "grunt-contrib-concat": "~0.3.0", 8 | "grunt-karma": "~0.6.2", 9 | "grunt-conventional-changelog": "~1.0.0", 10 | "karma-script-launcher": "~0.1.0", 11 | "karma-chrome-launcher": "~0.1.0", 12 | "karma-firefox-launcher": "~0.1.0", 13 | "karma-html2js-preprocessor": "~0.1.0", 14 | "karma-jasmine": "~0.1.3", 15 | "karma-requirejs": "~0.1.0", 16 | "karma-coffee-preprocessor": "~0.1.0", 17 | "karma-phantomjs-launcher": "~0.1.0", 18 | "karma": "~0.10.2" 19 | }, 20 | "scripts": { 21 | "test": "grunt test" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/angular-google-staticmaps.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * angular-google-staticmaps <%= pkg.version %> 3 | * Pascal Hartig, weluse GmbH, http://weluse.de/ 4 | * License: MIT 5 | */ 6 | (function () { 7 | 'use strict'; 8 | 9 | angular.module('wu.staticGmap', []) 10 | .controller('StaticGmapCtrl', function () { 11 | var BASE_URL = '//maps.googleapis.com/maps/api/staticmap?'; 12 | var STYLE_ATTRIBUTES = ['color', 'label', 'size']; 13 | 14 | this.makeMarkerStrings = function makeMarkerStrings(markers) { 15 | return markers.map(function (marker) { 16 | var str = Object.keys(marker).map(function (key) { 17 | if (STYLE_ATTRIBUTES.indexOf(key) > -1) { 18 | return key + ':' + marker[key] + '|'; 19 | } 20 | }).join(''); 21 | 22 | return str + marker.coords.join(','); 23 | }); 24 | }; 25 | 26 | this.buildSourceString = function buildSourceString(attrs, markers) { 27 | var markerStrings; 28 | 29 | if (markers) { 30 | if (!angular.isArray(markers)) { 31 | markers = [markers]; 32 | } 33 | markerStrings = this.makeMarkerStrings(markers); 34 | } 35 | 36 | var params = Object.keys(attrs).map(function (attr) { 37 | if (attr === 'markers' && markerStrings) { 38 | return Object.keys(markerStrings).map(function (key) { 39 | return 'markers=' + encodeURIComponent(markerStrings[key]); 40 | }).join('&'); 41 | } 42 | 43 | if (attr[0] !== '$' && attr !== 'alt') { 44 | return encodeURIComponent(attr) + '=' + encodeURIComponent(attrs[attr]); 45 | } 46 | }); 47 | 48 | return BASE_URL + params.reduce(function (a, b) { 49 | if (!a) { 50 | return b; 51 | } 52 | 53 | if (b !== undefined) { 54 | return a + '&' + b; 55 | } 56 | 57 | return a; 58 | }, ''); 59 | }; 60 | }) 61 | .directive('staticGmap', function ($parse) { 62 | return { 63 | template: 'Google Map', 64 | replace: true, 65 | restrict: 'E', 66 | controller: 'StaticGmapCtrl', 67 | scope: true, 68 | 69 | link: function postLink(scope, element, attrs, ctrl) { 70 | var el = element[0]; 71 | var markers = $parse(attrs.markers)(scope); 72 | 73 | if (!attrs.sensor) { 74 | throw new Error('The `sensor` attribute is required.'); 75 | } 76 | 77 | if (!attrs.size) { 78 | throw new Error('The `size` attribute is required.'); 79 | } 80 | 81 | var sizeBits = attrs.size.split('x'); 82 | if (sizeBits.length !== 2) { 83 | throw new Error('Size must be specified as `wxh`.'); 84 | } 85 | 86 | el.width = parseInt(sizeBits[0], 10); 87 | el.height = parseInt(sizeBits[1], 10); 88 | el.src = ctrl.buildSourceString(attrs, markers); 89 | } 90 | }; 91 | }); 92 | }()); 93 | -------------------------------------------------------------------------------- /test/spec/directive.coffee: -------------------------------------------------------------------------------- 1 | describe 'angular-google-staticmaps', -> 2 | 3 | controllerProvider = null 4 | scope = null 5 | 6 | beforeEach module('wu.staticGmap') 7 | beforeEach module((_$controllerProvider_) -> 8 | controllerProvider = _$controllerProvider_ 9 | null 10 | ) 11 | 12 | beforeEach inject(($rootScope) => 13 | @scope = $rootScope.$new() 14 | null 15 | ) 16 | 17 | describe 'static-gmap directive', => 18 | 19 | it 'should initialize', inject(($compile) => 20 | element = angular.element '' 21 | element = $compile(element)(@scope) 22 | ) 23 | 24 | it 'extract the size', inject(($compile) => 25 | element = angular.element '' 26 | element = $compile(element)(@scope) 27 | 28 | expect(element.attr('height')).toBe '10' 29 | expect(element.attr('width')).toBe '10' 30 | ) 31 | 32 | it 'should output an image', inject(($compile) => 33 | @scope.markers = [{ 34 | color: 'blue' 35 | label: 'S' 36 | coords: [53.556195, 9.9905748] 37 | }] 38 | 39 | element = angular.element ''' 40 | 41 | 42 | ''' 43 | element = $compile(element)(@scope) 44 | expect(element.attr('src')).toBe '//maps.googleapis.com/maps/api/staticmap?markers=color%3Ablue%7Clabel%3AS%7C53.556195%2C9.9905748&zoom=14&size=137x137&sensor=false' 45 | ) 46 | 47 | it 'should output an image for a markers literal', inject(($compile) => 48 | element = angular.element ''' 49 | 50 | 51 | ''' 52 | element = $compile(element)(@scope) 53 | expect(element.attr('src')).toBe '//maps.googleapis.com/maps/api/staticmap?markers=10%2C20&size=137x137&sensor=false' 54 | ) 55 | 56 | describe 'static-gmap controller', -> 57 | beforeEach(inject(($controller) => 58 | @ctrl = $controller 'StaticGmapCtrl' 59 | )) 60 | 61 | it 'should build a string for markers', => 62 | 63 | result = @ctrl.makeMarkerStrings([{ 64 | color: 'red' 65 | label: 'X' 66 | coords: [3.14, 4.28] 67 | }, { 68 | color: 'yellow' 69 | label: 'Y' 70 | coords: [10, 20] 71 | }]) 72 | 73 | expect(result.length).toBe 2 74 | expect(result[0]).toBe 'color:red|label:X|3.14,4.28' 75 | expect(result[1]).toBe 'color:yellow|label:Y|10,20' 76 | --------------------------------------------------------------------------------