forked from rueckstiess/geodndmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
217 lines (199 loc) · 6.84 KB
/
index.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
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
<!-- original example from https://developers.google.com/maps/documentation/javascript/examples/layer-data-dragndrop -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>Drag and Drop GeoJSON</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0; overflow: hidden; }
#map-canvas { height: 100% }
#drop-container {
display: none;
height: 100%;
width: 100%;
position: absolute;
z-index: 1;
top: 0px;
left: 0px;
padding: 20px;
background-color: rgba(100, 100, 100, 0.5);
}
#drop-silhouette {
color: white;
border: white dashed 8px;
height: calc(100% - 56px);
width: calc(100% - 56px);
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAAZiS0dEAGQAZABkkPCsTwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB90LHAIvICWdsKwAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAACdklEQVR42u3csU7icBzA8Xp3GBMSeRITH8JHMY7cRMvmVmXoE9TAcJubhjD4ApoiopgqDMWAKAgIcSAiCfxuwhwROVJbkPD9rP23ob8vpZCQKgoAAAAAAAAAAPDYyiK/eNM05bNtr6+vSjgcXiHxDMkE1WpVFvGcfpCVICAIQUAQgoAgBAFBCAKCgCAEAUEIAoIQBAQhCAgCghAEBCEICEIQEIQgIAgIQhAQhCAgCEFAEIKAICAIQUAQgoAgBAFBCDIzhmFINBo9/K6D0XVddnd3ZaneDY7jSCqVcn3SfjyeKRKJbJ2dnYllWbKUl2i5XJaXlxdJJBIy7yDHx8fy9vYm6XR6OWMM3d/fi4hIqVSSWCwmsw5ycHAgrVZLRETOz8+XO8ZQpVJ5H2Y6nRZN0/b9DqLruhSLxfd9MpkMMT6L0uv1JJlMih9BhveJwWDwvv7i4oIY4zw8PIwMtt1uSzweF6+CHB0dSbfbHVmbzWaJMcnj4+OHAd/d3cne3p64DWKapjw/P39Yd3l5SYxpVKvVsYO2LEtUVd2ZNoiu6+I4ztg1V1dXxPAiSq/Xk5OTk0k9pNVqyenp6ch94l+5XI4YbtRqNfHa9fX1t43xcwGa/Nnc3PwdDAY9OZht28rGxgZPvP6KSCSy9fT09OUrw7ZtPqa8jFKv113HuLm5IYbXVFXdcRPl9vaWGH5GaTQaU8fI5/PE8JumafvNZvO/MQqFAjFmJRqNHk6Ksqgx5vr1zzAM2d7edr3/6uqqsra2NnZbp9NR+v2+62OHQqG5zObXPIMEAgFlfX3dl2N79btl1viTA0FAEIKAIAQBAAAAAAAAsMz+Ai1bUgo6ebm8AAAAAElFTkSuQmCC');
background-repeat: no-repeat;
background-position: center;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=<insert API key here>&v=3.exp"></script>
<script type="text/javascript">
var map;
var bounds;
function initMap() {
// set up the map
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(0, 0),
zoom: 2
});
// set up the bounds
bounds = new google.maps.LatLngBounds();
}
/**
* converts arrays (of arrays (of arrays)) of coordinate pairs to LatLng objects.
* also updates the bounds for map auto-fit
**/
function coord2LatLng(arr, depth) {
if (depth === 0) {
// for Point
var p = new google.maps.LatLng(arr[1], arr[0]);
bounds.extend(p);
return p;
}
if (depth === 1) {
// for LineString
return arr.map(function (c) {
var p = new google.maps.LatLng(c[1], c[0]);
bounds.extend(p);
return p;
});
}
if (depth === 2) {
// for MultiLineString, Polygon
var paths = [];
arr.forEach(function (path) {
paths.push(path.map(function (coords) {
var p = new google.maps.LatLng(coords[1], coords[0]);
bounds.extend(p);
return p;
}));
});
return paths;
}
}
var colors = ['#1f77b4', '#ff7f0e','#2ca02c','#d62728','#9467bd','#8c564b','#e377c2','#7f7f7f','#bcbd22','#17becf']
var counter = 0;
function getNextColor() {
return colors[counter % colors.length];
counter++;
}
/**
* load GeoJSON string or object and render shape on map
**/
function loadGeoJson(geo) {
if (typeof geo === 'string') {
geo = JSON.parse(geo);
}
var color = getNextColor()
if (geo.type.toLowerCase() === 'polygon') {
var polygon = new google.maps.Polygon({
map: map,
paths: coord2LatLng(geo.coordinates, 2),
strokeColor: color,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: color,
fillOpacity: 0.30,
geodesic: true,
draggable: true
});
} else if (geo.type.toLowerCase() === 'linestring') {
var polyline = new google.maps.Polyline({
map: map,
path: coord2LatLng(geo.coordinates, 1),
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
geodesic: true
});
} else if (geo.type.toLowerCase() === 'point') {
var marker = new google.maps.Marker({
map: map,
position: coord2LatLng(geo.coordinates, 0)
});
} else if (geo.type.toLowerCase() === 'multipoint') {
var points = coord2LatLng(geo.coordinates, 1);
points.forEach(function (point) {
var marker = new google.maps.Marker({
map: map,
position: point
});
});
} else if (geo.type.toLowerCase() === 'geometrycollection') {
geo.geometries.forEach(loadGeoJson);
}
}
/**
* DOM (drag/drop) functions
**/
function initEvents() {
// set up the drag & drop events
var mapContainer = document.getElementById('map-canvas');
var dropContainer = document.getElementById('drop-container');
// first on common events
[mapContainer, dropContainer].forEach(function(container) {
container.addEventListener('drop', handleDrop, false);
container.addEventListener('dragover', showPanel, false);
});
// then map-specific events
mapContainer.addEventListener('dragstart', showPanel, false);
mapContainer.addEventListener('dragenter', showPanel, false);
// then the overlay specific events (since it only appears once drag starts)
dropContainer.addEventListener('dragend', hidePanel, false);
dropContainer.addEventListener('dragleave', hidePanel, false);
}
function showPanel(e) {
e.stopPropagation();
e.preventDefault();
document.getElementById('drop-container').style.display = 'block';
return false;
}
function hidePanel(e) {
document.getElementById('drop-container').style.display = 'none';
}
function handleDrop(e) {
e.preventDefault();
e.stopPropagation();
hidePanel(e);
var files = e.dataTransfer.files;
if (files.length) {
// process file(s) being dropped
// grab the file data from each file
for (var i = 0, file; file = files[i]; i++) {
var reader = new FileReader();
reader.onload = function(e) {
loadGeoJson(e.target.result);
map.fitBounds(bounds);
};
reader.onerror = function(e) {
console.error('reading failed');
};
reader.readAsText(file);
}
} else {
// process non-file (e.g. text or html) content being dropped
// grab the plain text version of the data
var plainText = e.dataTransfer.getData('text/plain');
if (plainText) {
loadGeoJson(plainText);
map.fitBounds(bounds);
}
}
// prevent drag event from bubbling further
return false;
}
google.maps.event.addDomListener(window, 'load', function() {
initMap();
initEvents();
});
</script>
</head>
<body>
<div id="map-canvas"></div>
<div id="drop-container"><div id="drop-silhouette"></div></div>
</body>
</html>