forked from kenta0g/sheet2gmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
124 lines (110 loc) · 3.49 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>sheet2gmap</title>
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#sidebar {
position: absolute;
top: 10%;
left: 72%;
width: 25%;
height: 40%;
border: 1px solid #666;
padding: 6px;
background-color: white;
font-family: Meriyo UI;
font-size: 14px;
}
</style>
</head>
<body>
<div id="map"></div>
<td><div id="sidebar"></div></td>
<script src="constants.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?language=ja®ion=JP&key=[API キーを入力してください]&callback=initMap" async defer></script>
<script>
var map;
var marker = [];
var infoWindow = [];
var windowOpened;
function parseData(data) {
var keys = data.values[0];
var markerData = [];
data.values.forEach(function(value, i) {
if (i > 0) {
var hash = {};
value.forEach(function(d, j) {
hash[keys[j]] = d;
});
markerData.push(hash);
}
});
return markerData;
}
function initMap() {
var target = document.getElementById('map');
var centerp = {lat: 35.6679191, lng: 139.4606805};
map = new google.maps.Map(target, {
center: centerp,
zoom: 7,
});
var request = new XMLHttpRequest();
request.open('GET', `https://sheets.googleapis.com/v4/spreadsheets/${SHEET_ID}/values/sheet1?key=${API_KEY}`, true);
request.responseType = 'json';
request.onload = function () {
var data = this.response;
var markerData = parseData(data);
setData(markerData);
};
request.send();
}
function addMarker(i, data) {
var markerLatLng = new google.maps.LatLng({
lat: Number(data['緯度']),
lng: Number(data['経度'])
});
marker[i] = new google.maps.Marker({
position: markerLatLng,
map: map
});
var info = '<div style="font-size:18px;font-weight:bold;margin-bottom:10px;">' + data['名称'] + '</div>';
infoWindow[i] = new google.maps.InfoWindow({
content: info
});
markerEvent(i);
}
function setData(markerData){
var sidebar_html = "";
for (var i = 0; i < markerData.length; i++) {
var latitude = markerData[i]['緯度'];
if (!latitude) { continue; }
addMarker(i, markerData[i]);
var name = markerData[i]['名称'];
sidebar_html += `<b>${i + 1}</b> <a href="javascript:openWindow(${i})">${name}<\/a><br />`;
}
document.getElementById("sidebar").innerHTML = `<a target="_blank" href="https://docs.google.com/spreadsheets/d/${SHEET_ID}/edit?usp=sharing">元データ(Google スプレッドシート)</a><br /><a target="_blank" href="https://github.com/champierre/sheet2gmap">ソースコード(GitHub)</a><br /><br />` + sidebar_html;
}
function markerEvent(i) {
marker[i].addListener('click', function() {
openWindow(i);
});
}
function openWindow(i) {
if(windowOpened){
windowOpened.close();
}
infoWindow[i].open(map, marker[i]);
windowOpened = infoWindow[i];
}
</script>
</body>
</html>