forked from timeu/google-map-markerclusterer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-map-defaulticon.html
153 lines (146 loc) · 4.59 KB
/
google-map-defaulticon.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="google-map-markercluster-icon-behavior.html">
<dom-module id="google-map-defaulticon">
<template>
<style>
:host {
position:absolute;
user-select: none;
}
.icon {
position:absolute;
left:0;
top:0;
}
.text {
position:absolute;
text-align:center;
font-family: Arial,sans-serif;
font-weight: bold;
font-style: normal;
text-decoration: none;
}
</style>
<img class="icon" id="icon" src$="{{_iconStyle.url}}"></img>
<div class="text" id="text">{{_computeText(markers)}}</div>
</template>
</dom-module>
<script>
/* global Markerclusterer */
(function() {
var BASE_IMAGE_URL = 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/';
var DEFAULT_STYLES = [
{
url: BASE_IMAGE_URL + 'm1.png',
width: '53px',
height: '52px',
textColor: 'black',
textSize: '11px'
},
{
url: BASE_IMAGE_URL + 'm2.png',
width: '56px',
height: '56px',
textColor: 'black',
textSize: '11px'
},
{
url: BASE_IMAGE_URL + 'm3.png',
width: '66px',
height: '66px',
textColor: 'black',
textSize: '12px'
},
{
url: BASE_IMAGE_URL + 'm4.png',
width: '78px',
height: '78px',
textColor: 'black',
textSize: '13px'
},
{
url: BASE_IMAGE_URL +'m5.png',
width: '90px',
height: '90px',
textColor: 'black',
textSize: '14px'
}
];
/*
* The `google-map-defaulticon` element is displayed by default when the user does not provide a custom cluster icon.
* @customElement
* @polymer
*/
class GoogleMapDefaultIcon extends ClusterIconBehavior(Polymer.Element) {
static get is() { return 'google-map-defaulticon'; }
static get properties() {
return {
/**
* The list of styles that should be applied to the cluster icon for the various
* cluster levels. If not set, the default styles will be used.
* Must be of object with following keys `{url,width,height,textColor,textSize}`
*/
styles : {
type:Array,
value: function() {return DEFAULT_STYLES;}
},
/**
* The active style for the current cluster level
*/
_iconStyle : {
type: Object,
value: function() {return {};}
}
}
}
static get observers() {
return [
'_updateCustom(markers,styles)'
]
}
/**
* Calculates the index of the active style that should be displayed for the current cluster.
*/
_calcActiveStyleIndex(markerLength,numStyles) {
var index = 0;
var dv = markerLength;
while (dv !== 0) {
dv = parseInt(dv / 10, 10);
index++;
}
return Math.max(0, Math.min(index, numStyles) - 1);
}
/**
* Returns the text that should be displayed
*/
_computeText(markers) {
return markers.length;
}
/**
* Observer function that is called when either the `markers` or `styles` is changed.
*/
_updateCustom(markers,styles) {
styles = styles || DEFAULT_STYLES;
// check also cluster.markers because remove on cluster will also delete the markers field
if (!markers || !styles ) {
return;
}
this._iconStyle = styles[this._calcActiveStyleIndex(markers.length,styles.length)];
var iconOffset = this._iconStyle.anchorIcon || [parseInt(this._iconStyle.height)/2+'px',parseInt(this._iconStyle.width)/2+'px'];
var textOffset = this._iconStyle.anchorText || ['0px','0px'];
this.$.text.style.left = textOffset[1];
this.$.text.style.top = textOffset[0];
this.$.text.style.width = this._iconStyle.width;
this.$.text.style.height = this._iconStyle.height;
this.$.text.style.lineHeight = this._iconStyle.height;
this.$.text.style.color = this._iconStyle.textColor;
this.$.text.style.fontSize = this._iconStyle.textSize;
this.style.left = '-'+iconOffset[1];
this.style.top = '-'+iconOffset[0];
this.$.text.style.position='absolute';
}
}
customElements.define('google-map-defaulticon', GoogleMapDefaultIcon);
})();
</script>
</dom-module>