Skip to content

Commit d009746

Browse files
committed
init
0 parents  commit d009746

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

ng-google-static-maps.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*!
2+
* ng-google-static-maps <%= pkg.version %>
3+
* Claudio La Barbera, https://claudiolabarbera.com
4+
* License: MIT
5+
*/
6+
(function () {
7+
'use strict';
8+
9+
angular.module('tbc.ngGoogleStaticMap', [])
10+
.controller('StaticGoogleMapCtrl', function () {
11+
var BASE_URL = '//maps.googleapis.com/maps/api/staticmap?';
12+
var STYLE_ATTRS = ['color', 'label', 'size', 'icon'];
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_ATTRS.indexOf(key) > -1) {
18+
return key + ':' + marker[key] + '|';
19+
}
20+
}).join('');
21+
22+
return str + marker.coords.join(',');
23+
});
24+
};
25+
26+
this.makeSrcString = function makeSrcString(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: '<img alt="Google Map">',
64+
replace: true,
65+
restrict: 'E',
66+
controller: 'StaticGoogleMapCtrl',
67+
scope: true,
68+
69+
link: function postLink(scope, elements, attrs, ctrl) {
70+
var element = elements[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 format must be `WIDTHxHEIGHT`.');
84+
}
85+
86+
element.width = parseInt(sizeBits[0], 10);
87+
element.height = parseInt(sizeBits[1], 10);
88+
element.src = ctrl.makeSrcString(attrs, markers);
89+
90+
scope.$watch(attrs.markers, function (newMarkers) {
91+
element.src = ctrl.makeSrcString(attrs, newMarkers);
92+
});
93+
}
94+
};
95+
});
96+
}());

0 commit comments

Comments
 (0)