-
Notifications
You must be signed in to change notification settings - Fork 12
/
angular-google-staticmaps.js
92 lines (77 loc) · 2.6 KB
/
angular-google-staticmaps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*!
* angular-google-staticmaps 0.0.1
* Pascal Hartig, weluse GmbH, http://weluse.de/
* License: MIT
*/
(function () {
'use strict';
angular.module('wu.staticGmap', [])
.controller('StaticGmapCtrl', function () {
var BASE_URL = '//maps.googleapis.com/maps/api/staticmap?';
var STYLE_ATTRIBUTES = ['color', 'label', 'size'];
this.makeMarkerStrings = function makeMarkerStrings(markers) {
return markers.map(function (marker) {
var str = Object.keys(marker).map(function (key) {
if (STYLE_ATTRIBUTES.indexOf(key) > -1) {
return key + ':' + marker[key] + '|';
}
}).join('');
return str + marker.coords.join(',');
});
};
this.buildSourceString = function buildSourceString(attrs, markers) {
var markerStrings;
if (markers) {
if (!angular.isArray(markers)) {
markers = [markers];
}
markerStrings = this.makeMarkerStrings(markers);
}
var params = Object.keys(attrs).map(function (attr) {
if (attr === 'markers' && markerStrings) {
return Object.keys(markerStrings).map(function (key) {
return 'markers=' + encodeURIComponent(markerStrings[key]);
}).join('&');
}
if (attr[0] !== '$' && attr !== 'alt') {
return encodeURIComponent(attr) + '=' + encodeURIComponent(attrs[attr]);
}
});
return BASE_URL + params.reduce(function (a, b) {
if (!a) {
return b;
}
if (b !== undefined) {
return a + '&' + b;
}
return a;
}, '');
};
})
.directive('staticGmap', function ($parse) {
return {
template: '<img alt="Google Map">',
replace: true,
restrict: 'E',
controller: 'StaticGmapCtrl',
scope: true,
link: function postLink(scope, element, attrs, ctrl) {
var el = element[0];
var markers = $parse(attrs.markers)(scope);
if (!attrs.sensor) {
throw new Error('The `sensor` attribute is required.');
}
if (!attrs.size) {
throw new Error('The `size` attribute is required.');
}
var sizeBits = attrs.size.split('x');
if (sizeBits.length !== 2) {
throw new Error('Size must be specified as `wxh`.');
}
el.width = parseInt(sizeBits[0], 10);
el.height = parseInt(sizeBits[1], 10);
el.src = ctrl.buildSourceString(attrs, markers);
}
};
});
}());