forked from novastra/GeoJSON-to-KML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
190 lines (171 loc) · 6.59 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Convert Maps GeoJson to MyMaps KML</title>
<meta name="description" content="Convert GeoJson to KML for My Maps">
<meta name="author" content="Novastra (Nicolas Alliot)">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body {
font-family: Century Gothic, arial, sans-serif;
text-align: center;
background-color: #F44336;
transition: background-color .5s ease-in;
}
.ending{
background-color: #009688;
}
h1{
color:#F3F3F3;
text-shadow: 1px 1px 3px #000;
text-align: center;
}
div{
text-align: center;
background-color: #F3F3F3;
color: #333;
margin: 0 25%;
padding: 20px;
}
a[type="button"]{
outline: none;
text-decoration: none;
color: #FFF;
background: #0c75ce;
cursor: pointer;
overflow: hidden;
text-align: center;
box-shadow: 0px 2px 10px 3px rgba(0,0,0,0.15);
text-shadow: none;
transition: all .3s ease-out;
padding: 10px;
font-size: 24px;
border:0px solid;
}
a[type="button"]:hover,
a[type="button"]:focus,
a[type="button"]:active{
background: #1490f9;
box-shadow: 0px 5px 10px 3px rgba(0,0,0,0.15);
}
@media screen and (max-width: 768px) {
div{
margin: 0 0;
}
}
</style>
</head>
<body>
<!--[if lt IE 8]>
<p>You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<h1>Convert Google Maps GeoJson to MyMaps KML</h1>
<a href="https://github.com/novastra/GeoJSON-to-KML"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
<div>
<p>Use this code to convert Google Maps saved addresses from <a target="_blank" href="https://takeout.google.com/settings/takeout">Google Takeout</a> to a KML file.</p>
<p>This code is 100% client-side so it is both safe for you (no collection) and for me (no injection). The file input creates a blob for the JSON file. When the blob is ready, an AJAX request is made to access the JSON content. Then it can be converted to KML.</p>
<p>The code is available and improvable on github.</p>
</div>
<form>
<input id="fileElem" accept="application/json" style="display:none" onchange="handleFiles(this.files)" type="file">
</form>
<a type="button" href="javascript:doClick()"><img src="http://www.gstatic.com/mapspro/images/stock/959-wht-circle-blank.png"></img> Select a Json file</a>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript">
var ultimateKML = "";
//xml special char replace
function specCharReplace(unsafe) {
return unsafe.replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
}
//DL
function downl(content) {
var fileName = 'myPlaces.kml';
var mimeType = 'application/vnd.google-earth.kml+xml';
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
// cross-browser dl link trigger
if (navigator.msSaveBlob) { // IE10
return navigator.msSaveBlob(new Blob([content], { type: mimeType }),
fileName);
}
else if ('download' in a) { //html5 A[download]
a.href = 'data:' + mimeType + ',' + encodeURIComponent(content);
a.setAttribute('download', fileName);
document.body.appendChild(a);
setTimeout(function() {
a.click();
document.body.removeChild(a);
}, 66);
return true;
}
else { //do iframe dataURL download (old ch+FF):
var f = document.createElement('iframe');
document.body.appendChild(f);
f.src = 'data:' + mimeType + ',' + encodeURIComponent(content);
setTimeout(function() {
document.body.removeChild(f);
}, 333);
return true;
}
};
//button for input and self validation
function doClick() {
var el = document.getElementById("fileElem");
if (el) {
el.click();
}
}
// Sending a JSON file as blob then get the blob by ajax request to blob data url
function handleFiles(files) {
if (!files.length) {
alert("No file selected")
}
else if (!(files[0].type==='application/json')) {
alert("The file must be of JSON type")
}
else {
var apiReq = window.URL.createObjectURL(files[0]);
$.ajax({
xhrFields: {
withCredentials: true
},
type: "GET",
headers: {'X-Requested-With': 'XMLHttpRequest'},
crossDomain: true,
url: apiReq
}).done(function (data) {
//first part of the KML
ultimateKML = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document><name> MyPlaces </name><open>1</open><Style id="INSERT"><IconStyle><color>5014B4FA</color><scale>1.1</scale><Icon><href>http://www.gstatic.com/mapspro/images/stock/960-wht-star-blank.png</href></Icon></IconStyle></Style>';
//get data
for (x in data.features){
coord = data.features[x].geometry.coordinates;
title = specCharReplace(data.features[x].properties.Title);
address = data.features[x].properties.Address;
var place = "<Placemark><name>" + title + "</name><address>" + address + "</address><styleUrl>#INSERT</styleUrl><MultiGeometry><Point><coordinates>" + coord + "</coordinates></Point></MultiGeometry></Placemark>"
ultimateKML += place;
}
//ending the kml file
ultimateKML += '</Document></kml>';
//styling the page
$("body").toggleClass("ending");
$("div").fadeOut(500);
$("h1").text("Your KML file is ready");
$("a[type='button'").replaceWith('<a type="button" href="#" onClick="javascript:downl(ultimateKML)"><img src="https://www.gstatic.com/mapspro/images/stock/962-wht-diamond-blank.png"></img> Download the KML file</a>');
//trigger download at end
downl(ultimateKML);
})
}
}
</script>
</body>
</html>