-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.overlay.google-map.js
149 lines (135 loc) · 5.8 KB
/
custom.overlay.google-map.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
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
/*
* Name: Google Map CustomOverlay
* Version: 1.0
* Author: Khandaker Jim
* Author URI: https://github.com/Khandakerjim
* link: https://github.com/khandakerjim/Google-Map-Custom-Overlay
## Copyright (c) 2018 - Khandaker Jim - https://github.com/khandakerjim/Google-Map-Custom-Overlay
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall
be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
function CustomOverlay( opts ) {
google.maps.OverlayView.call( this );
this.latlng_ = opts.latlng;
this.map_ = opts.map;
this.content = opts.content;
this.offsetVertical_ = opts.verticalOffset;
this.offsetHorizontal_ = opts.horizontalOffset;
this.height_ = 105;
this.width_ = 170;
var me = this;
this.boundsChangedListener_ =
google.maps.event.addListener( this.map_, "bounds_changed", function () {
return me.panMap.apply( me );
});
this.setMap( this.map_ );
};
CustomOverlay.prototype = new google.maps.OverlayView();
CustomOverlay.prototype.remove = function () {
if ( this.div_ ) {
this.div_.parentNode.removeChild( this.div_ );
this.div_ = null;
}
};
CustomOverlay.prototype.draw = function () {
this.createElement();
if ( !this.div_ ) return;
var pixPosition = this.getProjection().fromLatLngToDivPixel( this.latlng_ );
if ( !pixPosition ) return;
this.div_.style.width = this.width_ + "px";
this.div_.style.left = ( pixPosition.x + this.offsetHorizontal_ ) + "px";
this.div_.style.height = this.height_ + "px";
this.div_.style.top = ( pixPosition.y + this.offsetVertical_ ) + "px";
this.div_.style.display = 'block';
};
CustomOverlay.prototype.createElement = function () {
var panes = this.getPanes();
var div = this.div_;
if ( !div ) {
div = this.div_ = document.createElement( 'div' );
div.className = 'google-map-overlay';
var contentWrapperDiv = document.createElement( 'div' );
contentWrapperDiv.className = 'overlay-body';
div.appendChild( contentWrapperDiv );
var overlayClose = document.createElement( 'div' );
overlayClose.className = 'overlayclose';
contentWrapperDiv.appendChild( overlayClose );
var contentDiv = document.createElement( 'div' );
contentDiv.className = 'overlay-content';
contentDiv.innerHTML = this.content;
var closeBox = document.createElement( 'div' );
closeBox.className = 'popup-corner';
var divShape = document.createElement( 'div' );
closeBox.appendChild( divShape );
div.appendChild( closeBox );
function removeCustomOverlay( ib ) {
return function () {
ib.setMap(null);
};
}
google.maps.event.addDomListener( overlayClose, 'click', removeCustomOverlay(this));
contentWrapperDiv.appendChild(contentDiv);
div.style.display = 'none';
panes.floatPane.appendChild(div);
this.panMap();
} else if (div.parentNode != panes.floatPane) {
div.parentNode.removeChild(div);
panes.floatPane.appendChild(div);
} else {
}
}
CustomOverlay.prototype.panMap = function () {
var map = this.map_;
var bounds = map.getBounds();
if ( !bounds ) return;
var position = this.latlng_;
var iwWidth = this.width_;
var iwHeight = this.height_;
var iwOffsetX = this.offsetHorizontal_;
var iwOffsetY = this.offsetVertical_;
var padX = 0;
var padY = 0;
var mapDiv = map.getDiv();
var mapWidth = mapDiv.offsetWidth;
var mapHeight = mapDiv.offsetHeight;
var boundsSpan = bounds.toSpan();
var longSpan = boundsSpan.lng();
var latSpan = boundsSpan.lat();
var degPixelX = longSpan / mapWidth;
var degPixelY = latSpan / mapHeight;
var mapWestLng = bounds.getSouthWest().lng();
var mapEastLng = bounds.getNorthEast().lng();
var mapNorthLat = bounds.getNorthEast().lat();
var mapSouthLat = bounds.getSouthWest().lat();
var iwWestLng = position.lng() + (iwOffsetX - padX) * degPixelX;
var iwEastLng = position.lng() + (iwOffsetX + iwWidth + padX) * degPixelX;
var iwNorthLat = position.lat() - (iwOffsetY - padY) * degPixelY;
var iwSouthLat = position.lat() - (iwOffsetY + iwHeight + padY) * degPixelY;
var shiftLng =
( iwWestLng < mapWestLng ? mapWestLng - iwWestLng : 0 ) +
( iwEastLng > mapEastLng ? mapEastLng - iwEastLng : 0 );
var shiftLat =
( iwNorthLat > mapNorthLat ? mapNorthLat - iwNorthLat : 0 ) +
( iwSouthLat < mapSouthLat ? mapSouthLat - iwSouthLat : 0 );
var center = map.getCenter();
var centerX = center.lng() - shiftLng;
var centerY = center.lat() - shiftLat;
map.setCenter(new google.maps.LatLng(centerY, centerX));
google.maps.event.removeListener(this.boundsChangedListener_);
this.boundsChangedListener_ = null;
};