Skip to content

Commit e26d2ab

Browse files
committed
Improve custom markers performance
1 parent 9fcd927 commit e26d2ab

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

directives/custom-marker.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,22 @@
2929
'use strict';
3030
var parser, $timeout, $compile, NgMap;
3131

32+
var supportedTransform = (function getSupportedTransform() {
33+
var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' ');
34+
var div = document.createElement('div');
35+
for(var i = 0; i < prefixes.length; i++) {
36+
if(div && div.style[prefixes[i]] !== undefined) {
37+
return prefixes[i];
38+
}
39+
}
40+
return false;
41+
})();
42+
3243
var CustomMarker = function(options) {
3344
options = options || {};
3445

3546
this.el = document.createElement('div');
36-
this.el.style.display = 'inline-block';
47+
this.el.style.display = 'block';
3748
this.el.style.visibility = "hidden";
3849
this.visible = true;
3950
for (var key in options) { /* jshint ignore:line */
@@ -46,8 +57,11 @@
4657
CustomMarker.prototype = new google.maps.OverlayView();
4758

4859
CustomMarker.prototype.setContent = function(html, scope) {
60+
console.log('input html', html);
4961
this.el.innerHTML = html;
5062
this.el.style.position = 'absolute';
63+
this.el.style.top = 0;
64+
this.el.style.left = 0;
5165
if (scope) {
5266
$compile(angular.element(this.el).contents())(scope);
5367
}
@@ -75,8 +89,12 @@
7589
var posPixel = _this.getProjection().fromLatLngToDivPixel(_this.position);
7690
var x = Math.round(posPixel.x - (_this.el.offsetWidth/2));
7791
var y = Math.round(posPixel.y - _this.el.offsetHeight - 10); // 10px for anchor
78-
_this.el.style.left = x + "px";
79-
_this.el.style.top = y + "px";
92+
if (supportedTransform) {
93+
_this.el.style[supportedTransform] = "translate(" + x + "px, " + y + "px)";
94+
} else {
95+
_this.el.style.left = x + "px";
96+
_this.el.style.top = y + "px";
97+
}
8098
_this.el.style.visibility = "visible";
8199
};
82100
if (_this.el.offsetWidth && _this.el.offsetHeight) {
@@ -90,15 +108,20 @@
90108

91109
CustomMarker.prototype.setZIndex = function(zIndex) {
92110
zIndex && (this.zIndex = zIndex); /* jshint ignore:line */
93-
this.el.style.zIndex = this.zIndex;
111+
(this.el.style.zIndex !== this.zIndex) && (this.el.style.zIndex = this.zIndex);
94112
};
95113

96114
CustomMarker.prototype.getVisible = function() {
97115
return this.visible;
98116
};
99117

100118
CustomMarker.prototype.setVisible = function(visible) {
101-
this.el.style.display = visible ? 'inline-block' : 'none';
119+
if (this.el.style.display === 'none' && visible)
120+
{
121+
this.el.style.display = 'block';
122+
} else if (this.el.style.display !== 'none' && !visible) {
123+
this.el.style.display = 'none';
124+
}
102125
this.visible = visible;
103126
};
104127

@@ -149,23 +172,26 @@
149172
console.log("custom-marker options", options);
150173
var customMarker = new CustomMarker(options);
151174

152-
$timeout(function() { //apply contents, class, and location after it is compiled
175+
// Do we really need a timeout with $scope.$apply() here?
176+
setTimeout(function() { //apply contents, class, and location after it is compiled
153177

154178
scope.$watch('[' + varsToWatch.join(',') + ']', function() {
155179
customMarker.setContent(orgHtml, scope);
156180
}, true);
157181

158182
customMarker.setContent(element[0].innerHTML, scope);
159-
var classNames = element[0].firstElementChild.className;
183+
var classNames =
184+
(element[0].firstElementChild) && (element[0].firstElementChild.className || '');
185+
customMarker.class && (classNames += " " + customMarker.class);
160186
customMarker.addClass('custom-marker');
161-
customMarker.addClass(classNames);
187+
classNames && customMarker.addClass(classNames);
162188
console.log('customMarker', customMarker, 'classNames', classNames);
163189

164190
if (!(options.position instanceof google.maps.LatLng)) {
165191
NgMap.getGeoLocation(options.position).then(
166-
function(latlng) {
167-
customMarker.setPosition(latlng);
168-
}
192+
function(latlng) {
193+
customMarker.setPosition(latlng);
194+
}
169195
);
170196
}
171197

0 commit comments

Comments
 (0)