-
Notifications
You must be signed in to change notification settings - Fork 12
/
google-map-overlayview-behavior.html
148 lines (134 loc) · 4.2 KB
/
google-map-overlayview-behavior.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
<script>
/**
* Use `GoogleMapOverlayViewBehavior` to implement elements that should be displayed on a `google-map` using an overlayview
* @polymer
* @mixinFunction
*/
GoogleMapOverlayViewBehavior = function(superClass) {
return class extends superClass {
static get properties() {
return {
/**
* A Maps API object.
*/
map: {
type: Object,
value: null,
observer: '_mapChanged'
},
/**
* A boolean flag to set the visiblity
*/
visible: {
type: Boolean,
value: true
},
}
}
ready() {
super.ready();
this._initOverlay();
}
/**
* Initializes the overlay if the Google Maps API was properly loaded.
* Sets the callbacks (onAdd, draw and onRemove) to the current instance
*/
_initOverlay() {
if (typeof google === 'object' && typeof google.maps === 'object') {
if (this.overlay && this.overlay instanceof google.maps.OverlayView) {
return;
}
OverlayView.prototype = new google.maps.OverlayView();
OverlayView.prototype.onAdd = this.onAdd.bind(this);
OverlayView.prototype.draw = this.draw.bind(this);
OverlayView.prototype.onRemove = this.onRemove.bind(this);
this.overlay = new OverlayView(this.map);
}
}
/**
* OverlayView object constructor.
*/
_overlayView(map) {
if (map && map instanceof google.maps.Map) {
this.map = map;
}
}
/**
* Callback when the map is changed. By default this will call `setMap` on the `OverlayView`
*/
_mapChanged(map) {
this._initOverlay();
if (this.overlay && this.overlay instanceof google.maps.OverlayView) {
this.overlay.setMap(map);
}
}
/**
* draw callback when the overlayview is drawn
*/
draw() {}
/**
* onAdd callback when the overlayview is added to the map.
*/
onAdd() {
var panes = this.overlay.getPanes();
panes.overlayLayer.appendChild(this);
}
/**
* onRemove callback when the overlayview is removed from the map.
*/
onRemove() {
if (this.parentNode) {
this.parentNode.removeChild(this);
}
}
/**
* Returns the position at which to place the DIV depending on the latlng.
*
* @param {google.maps.LatLng} latlng The position in latlng.
* @return {google.maps.Point} The position in pixels.
*/
getPosFromLatLng(latlng) {
var pos = this.overlay.getProjection().fromLatLngToDivPixel(latlng);
pos.x = parseInt(pos.x, 10);
pos.y = parseInt(pos.y, 10);
return pos;
}
/**
* Returns the current bounds extended by the grid size.
*
* @param {google.maps.LatLngBounds} bounds The bounds to extend.
* @return {google.maps.LatLngBounds} The extended bounds.
*/
getExtendedBounds(bounds, size) {
var projection = this.overlay.getProjection();
// Turn the bounds into latlng.
var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
bounds.getNorthEast().lng());
var bl = new google.maps.LatLng(bounds.getSouthWest().lat(),
bounds.getSouthWest().lng());
// Convert the points to pixels and the extend out by the grid size.
var trPix = projection.fromLatLngToDivPixel(tr);
trPix.x += size;
trPix.y -= size;
var blPix = projection.fromLatLngToDivPixel(bl);
blPix.x -= size;
blPix.y += size;
// Convert the pixel points back to LatLng
var ne = projection.fromDivPixelToLatLng(trPix);
var sw = projection.fromDivPixelToLatLng(blPix);
// Extend the bounds to contain the new bounds.
bounds.extend(ne);
bounds.extend(sw);
return bounds;
}
}
}
/**
* OverlayView object constructor.
*/
function OverlayView(map) {
if (map && map instanceof google.maps.Map) {
this.map = map;
}
};
</script>