-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.js
227 lines (204 loc) · 7.02 KB
/
geo.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { colorTopic } from "./misc.js"
const strokeColor = '#F71'
const shapeOptions = {
strokeColor: strokeColor,
strokeOpacity: 0.7,
fillColor: strokeColor,
fillOpacity: 0.025,
draggable: true,
clickable: true,
editable: true,
visible: true,
}
const rectColor = "#00c895"
const subRectangleOptions = {
strokeColor: rectColor,
strokeOpacity: 0.5,
strokeWeight: 1,
fillColor: rectColor,
fillOpacity: 0.05,
zIndex: 1000000, //factor,
clickable: true,
}
const shapes = []
const curtRangeRectangles = []
let curtGeoFilterReply
let isDragging = false // the user is dragging the shapes now
let map;
let drawingManager;
let cancelDrawing = false // the use press ESC to cancel current shape
let onFilteringShapesChanged
let showAllRanges = false // whether to show ranged rectangles
let rangRectTag
let rangeTopicTag
const geo = {
init: function (_map, _onShapesChanged) {
map = _map
onFilteringShapesChanged = _onShapesChanged
geo.setupDrawingManager()
document.getElementById("btn-circle").addEventListener('click',
() => { geo.startDrawing(google.maps.drawing.OverlayType.CIRCLE) })
document.getElementById("btn-rect").addEventListener('click',
() => { geo.startDrawing(google.maps.drawing.OverlayType.RECTANGLE) })
document.getElementById("btn-polygon").addEventListener('click',
() => { geo.startDrawing(google.maps.drawing.OverlayType.POLYGON) })
document.getElementById("btn-remove").addEventListener('click', geo.removeAllShapes)
const showRangesCheckbox = document.getElementById("show_ranges")
showRangesCheckbox.checked = showAllRanges
showRangesCheckbox.addEventListener('change', () => {
showAllRanges = showRangesCheckbox.checked
if (showAllRanges) {
geo.drawAllRangeRectangles()
} else {
geo.removeAllRangeRectangles()
}
})
rangRectTag = document.getElementById("range-rect")
rangeTopicTag = document.getElementById("range-topic")
},
closeDrawingManager() {
drawingManager.setDrawingMode(null)
drawingManager.setMap(null)
},
setupDrawingManager() {
drawingManager = new google.maps.drawing.DrawingManager({
circleOptions: shapeOptions,
rectangleOptions: shapeOptions,
polygonOptions: shapeOptions,
drawingMode: null,
drawingControl: false,
drawingControlOptions: { position: google.maps.ControlPosition.TOP_CENTER, }
})
drawingManager.addListener('circlecomplete', function (circle) {
geo.onNewShape(circle, google.maps.drawing.OverlayType.CIRCLE)
circle.addListener('radius_changed', geo.onShapeChanged);
circle.addListener('center_changed', geo.onShapeChanged);
})
drawingManager.addListener('rectanglecomplete', function (rect) {
geo.onNewShape(rect, google.maps.drawing.OverlayType.RECTANGLE)
rect.addListener('bounds_changed', geo.onShapeChanged);
})
drawingManager.addListener('polygoncomplete', function (polygon) {
geo.onNewShape(polygon, google.maps.drawing.OverlayType.POLYGON)
google.maps.event.addListener(polygon.getPath(), 'set_at', geo.onShapeChanged);
google.maps.event.addListener(polygon.getPath(), 'insert_at', geo.onShapeChanged);
google.maps.event.addListener(polygon.getPath(), 'remove_at', geo.onShapeChanged);
})
document.addEventListener('keydown', (event) => {
if (event.key === "Escape") {
cancelDrawing = true
geo.closeDrawingManager()
}
})
},
startDrawing: function (drawingMode) {
cancelDrawing = false
drawingManager.setOptions({
map,
drawingMode: drawingMode,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [drawingMode]
}
})
},
onNewShape(shape, drawingMode) {
if (cancelDrawing) {
shape.setMap(null)
return
}
geo.closeDrawingManager()
shapes.push(shape)
shape.shapeType = drawingMode
geo.onShapeChanged()
shape.addListener('dragstart', () => { isDragging = true });
shape.addListener('dragend', () => { isDragging = false; geo.onShapeChanged() })
},
onShapeChanged() {
if (isDragging) { return }
let result = []
shapes.forEach((shape) => {
let shapeObj
switch (shape.shapeType) {
case google.maps.drawing.OverlayType.CIRCLE:
shapeObj = {
type: "Ellipse", // circle on google map is actually an ellipse
bounds: shape.getBounds(),
}
break
case google.maps.drawing.OverlayType.RECTANGLE:
shapeObj = {
type: "Rectangle",
bounds: shape.getBounds(),
}
break
case google.maps.drawing.OverlayType.POLYGON:
shapeObj = {
type: "Polygon",
coordinates: shape.getPath().getArray(),
}
break
default:
return
}
result.push(shapeObj)
})
onFilteringShapesChanged(result)
},
removeAllShapes() {
for (let shape = shapes.pop(); 'undefined' != typeof shape; shape = shapes.pop()) {
shape.setMap(null)
}
geo.removeAllRangeRectangles()
onFilteringShapesChanged([])
},
updateRangeRectangles(reply) {
geo.removeAllRangeRectangles()
curtGeoFilterReply = reply
geo.drawAllRangeRectangles()
},
removeAllRangeRectangles() {
for (let rect = curtRangeRectangles.pop(); 'undefined' != typeof rect; rect = curtRangeRectangles.pop()) {
rect.setMap(null)
}
},
drawAllRangeRectangles() {
if (!showAllRanges) { return }
if (curtGeoFilterReply === null) { return }
if (!("ranges" in curtGeoFilterReply)) { return }
for (let i = 0; i < curtGeoFilterReply.ranges.length; i++) {
const range = curtGeoFilterReply.ranges[i]
const x1 = range.sign.X * range.coord.X
const x2 = range.sign.X * (range.coord.X + range.unit.X)
const y1 = range.sign.Y * range.coord.Y
const y2 = range.sign.Y * (range.coord.Y + range.unit.Y)
const rect = new google.maps.Rectangle(Object.assign({
map: map,
bounds: {
north: y1 > y2 ? y1 : y2,
south: y1 < y2 ? y1 : y2,
east: x1 > x2 ? x1 : x2,
west: x1 < x2 ? x1 : x2,
},
}, subRectangleOptions))
const temp = curtGeoFilterReply.topicPattern.replace('{lat}', range.filtering.Y)
rect["topic"] = temp.replace("{lng}", range.filtering.X)
rect.addListener("mouseover", function () {
rect.setOptions({ fillOpacity: 0.1, strokeOpacity: 1 });
rangeTopicTag.innerHTML = colorTopic(rect["topic"])
rangRectTag.style.display = "block"
});
rect.addListener("mousemove", function (event) {
rangRectTag.style.left = (event.domEvent.x + 10) + 'px';
rangRectTag.style.top = (event.domEvent.y + 10) + 'px';
});
rect.addListener("mouseout", function () {
rect.setOptions({ fillOpacity: 0.05, strokeOpacity: 0.5 });
rangRectTag.style.display = "none"
});
curtRangeRectangles.push(rect)
}
},
}
export { geo as default }