-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdrawMap.js
171 lines (142 loc) · 5.14 KB
/
drawMap.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
var mapOptions;
var map;
var coordinates = []
let new_coordinates = []
let lastElement
function InitMap() {
var location = new google.maps.LatLng(28.620585, 77.228609)
mapOptions = {
zoom: 12,
center: location,
mapTypeId: google.maps.MapTypeId.RoadMap
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions)
var all_overlays = [];
var selectedShape;
var drawingManager = new google.maps.drawing.DrawingManager({
//drawingMode: google.maps.drawing.OverlayType.MARKER,
//drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
//google.maps.drawing.OverlayType.MARKER,
//google.maps.drawing.OverlayType.CIRCLE,
google.maps.drawing.OverlayType.POLYGON,
//google.maps.drawing.OverlayType.RECTANGLE
]
},
markerOptions: {
//icon: 'images/beachflag.png'
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 0.2,
strokeWeight: 3,
clickable: false,
editable: true,
zIndex: 1
},
polygonOptions: {
clickable: true,
draggable: false,
editable: true,
// fillColor: '#ffff00',
fillColor: '#ADFF2F',
fillOpacity: 0.5,
},
rectangleOptions: {
clickable: true,
draggable: true,
editable: true,
fillColor: '#ffff00',
fillOpacity: 0.5,
}
});
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
//to disable drawing tools
function stopDrawing() {
drawingManager.setMap(null);
}
function setSelection(shape) {
clearSelection();
stopDrawing()
selectedShape = shape;
shape.setEditable(true);
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
drawingManager.setMap(map);
coordinates.splice(0, coordinates.length)
document.getElementById('info').innerHTML = ""
}
}
function CenterControl(controlDiv, map) {
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = '#fff';
controlUI.style.border = '2px solid #fff';
controlUI.style.borderRadius = '3px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
controlUI.style.cursor = 'pointer';
controlUI.style.marginBottom = '22px';
controlUI.style.textAlign = 'center';
controlUI.title = 'Select to delete the shape';
controlDiv.appendChild(controlUI);
// Set CSS for the control interior.
var controlText = document.createElement('div');
controlText.style.color = 'rgb(25,25,25)';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize = '16px';
controlText.style.lineHeight = '38px';
controlText.style.paddingLeft = '5px';
controlText.style.paddingRight = '5px';
controlText.innerHTML = 'Delete Selected Area';
controlUI.appendChild(controlText);
//to delete the polygon
controlUI.addEventListener('click', function () {
deleteSelectedShape();
});
}
drawingManager.setMap(map);
var getPolygonCoords = function (newShape) {
coordinates.splice(0, coordinates.length)
var len = newShape.getPath().getLength();
for (var i = 0; i < len; i++) {
coordinates.push(newShape.getPath().getAt(i).toUrlValue(6))
}
document.getElementById('info').innerHTML = coordinates
}
google.maps.event.addListener(drawingManager, 'polygoncomplete', function (event) {
event.getPath().getLength();
google.maps.event.addListener(event, "dragend", getPolygonCoords(event));
google.maps.event.addListener(event.getPath(), 'insert_at', function () {
getPolygonCoords(event)
});
google.maps.event.addListener(event.getPath(), 'set_at', function () {
getPolygonCoords(event)
})
})
google.maps.event.addListener(drawingManager, 'overlaycomplete', function (event) {
all_overlays.push(event);
if (event.type !== google.maps.drawing.OverlayType.MARKER) {
drawingManager.setDrawingMode(null);
var newShape = event.overlay;
newShape.type = event.type;
google.maps.event.addListener(newShape, 'click', function () {
setSelection(newShape);
});
setSelection(newShape);
}
})
var centerControlDiv = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map);
centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(centerControlDiv);
}
InitMap()