-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoJSON_editor.html
179 lines (146 loc) · 5.39 KB
/
GeoJSON_editor.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
<!DOCTYPE html>
<!-- saved from url=(0055)http://blog.sallarp.com/geojson-google-maps-editor.html -->
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style type="text/css">@media print { .gmnoprint { display:none }}@media screen { .gmnoscreen { display:none }}</style>
<title>GeoJSON editor</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map_canvas {
height: 80%;
}
#map_io {
height: 20%;
}
#geoJSON {
height: 100%;
width: 80%;
float: left;
}
#loadJsonButton {
margin: 5px;
}
</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=drawing"></script>
<script src="./json2.js"></script>
<script type="text/javascript">
var allOverlays = new Array();
var map = null;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(30, 1),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.MARKER
]
},
polygonOptions: {
editable: true,
zIndex: 1
},
markerOptions: {
draggable:true
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(overlay) {
addMapOverlay(overlay);
});
google.maps.event.addListener(drawingManager, 'markercomplete', function(overlay) {
document.getElementById("lastMarker").innerHTML = overlay.position.lng() + ',' + overlay.position.lat();
google.maps.event.addListener(overlay, 'dragend', function() {
document.getElementById("lastMarker").innerHTML = overlay.position.lng() + ',' + overlay.position.lat();
});
});
}
function addMapOverlay(overlay) {
allOverlays.push(overlay);
overlay.setMap(map);
google.maps.event.addListener(overlay.getPath(), 'insert_at', function() {
dumpGeoJSON(allOverlays);
});
google.maps.event.addListener(overlay.getPath(), 'remove_at', function() {
dumpGeoJSON(allOverlays);
});
google.maps.event.addListener(overlay.getPath(), 'set_at', function() {
dumpGeoJSON(allOverlays);
});
dumpGeoJSON(allOverlays);
}
function dumpGeoJSON(overlays) {
var coordinates = new Array();
for (var i = 0; i < overlays.length; i++) {
var polygon = new Array();
var vertices = overlays[i].getPath();
for (var j = 0; j < vertices.length; j++) {
var xy = vertices.getAt(j);
var lngLat = new Array(xy.lng(), xy.lat());
polygon.push(lngLat);
}
// Add the first polygon again to close the polygon.
var xy = vertices.getAt(0);
polygon.push(new Array(xy.lng(), xy.lat()));
coordinates.push(new Array(polygon));
}
var output = document.getElementById('geoJSON');
var geoJSON = { 'type': 'MultiPolygon', 'coordinates' : coordinates };
output.value = JSON.stringify(geoJSON, null, '\t');
}
function loadGeoJSON() {
var textarea = document.getElementById('geoJSON');
var geoJSON;
try {
geoJSON = JSON.parse(textarea.value);
}
catch(e) {
alert('json parse failed: \n' + e);
return;
}
if (geoJSON.type != 'MultiPolygon') {
alert('Your json does not appear to be a MultiPolygon type!');
return;
}
if (!geoJSON.coordinates) {
alert('Your json appear to lack a coordinates attribute!');
return;
}
// Remove all existing overlays
for (i in allOverlays) {
allOverlays[i].setMap(null);
}
allOverlays.length = 0;
for (i in geoJSON.coordinates) {
var polyCoords = new Array();
for (j in geoJSON.coordinates[i][0]) {
var coords = geoJSON.coordinates[i][0][j];
polyCoords.push(new google.maps.LatLng(coords[1], coords[0]));
}
var overlay = new google.maps.Polygon({ paths: polyCoords, editable: true });
addMapOverlay(overlay);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"> </div>
<div id="map_io">
<textarea id="geoJSON"></textarea>
<input id="loadJsonButton" type="button" value="Load GeoJSON" onclick="loadGeoJSON();"><br>
Last marker : <span id="lastMarker"/>
</div>
</body></html>