-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (99 loc) · 3.83 KB
/
Copy pathscript.js
File metadata and controls
117 lines (99 loc) · 3.83 KB
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
var map;
var markers = [];
var polyline;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 0, lng: 0 },
zoom: 2
});
}
function showMaps() {
clearMap();
document.getElementById('map-container').style.display = 'block';
fetchDataAndUpdateMap();
}
function showCoordinates() {
clearMap();
document.getElementById('map-container').style.display = 'none';
fetchCoordinates();
}
function fetchDataAndUpdateMap() {
const apiKey = 'AIzaSyAMDCqRKW6XPHJvxKYqvgr24r4RfhrjAss';
const spreadsheetId = '1HObmilSg2FHjOtueg9tvRn9N7lwkZAt8RYnRw08v-80';
const apiUrl = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/Sheet1?key=${apiKey}`;
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => updateMap(data.values))
.catch(error => console.error('Error fetching data:', error));
}
function fetchCoordinates() {
const apiKey = 'AIzaSyAMDCqRKW6XPHJvxKYqvgr24r4RfhrjAss';
const spreadsheetId = '1HObmilSg2FHjOtueg9tvRn9N7lwkZAt8RYnRw08v-80';
const apiUrl = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/Sheet1?key=${apiKey}`;
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => displayCoordinates(data.values))
.catch(error => console.error('Error fetching data:', error));
}
function updateMap(data) {
// Assuming your data has columns: Name, Latitude, Longitude
// Sort the data array based on latitude in descending order
data.sort((a, b) => parseFloat(b[1]) - parseFloat(a[1]));
// Take the top two coordinates
const topCoordinates = data.slice(0, 2);
// Create markers for the top two coordinates
topCoordinates.forEach((coord, index) => {
const name = coord[0];
const latitude = parseFloat(coord[2]); // Change to 3rd column for latitude
const longitude = parseFloat(coord[3]); // Change to 4th column for longitude
if (!isNaN(latitude) && !isNaN(longitude)) {
const marker = new google.maps.Marker({
position: { lat: latitude, lng: longitude },
map: map,
label: (index + 1).toString(), // Display the number as label
title: name
});
markers.push(marker);
}
});
// Create a Polyline between the top two coordinates
const coordinates = topCoordinates.map(coord => {
const latitude = parseFloat(coord[2]); // Change to 3rd column for latitude
const longitude = parseFloat(coord[3]); // Change to 4th column for longitude
return { lat: latitude, lng: longitude };
});
polyline = new google.maps.Polyline({
path: coordinates,
geodesic: true,
strokeColor: '#FF0000', // Line color
strokeOpacity: 1.0,
strokeWeight: 2
});
polyline.setMap(map);
}
function displayCoordinates(data) {
const coordinatesList = data.map(coord => {
const timestamp = coord[1]; // Assuming timestamp is in the 2nd column
const latitude = parseFloat(coord[2]); // Change to 3rd column for latitude
const longitude = parseFloat(coord[3]); // Change to 4th column for longitude
return `<p>Timestamp: ${timestamp}, Latitude: ${latitude}, Longitude: ${longitude}</p>`;
});
document.getElementById('map').innerHTML = coordinatesList.join('');
}
function clearMap() {
markers.forEach(marker => marker.setMap(null));
markers = [];
if (polyline) {
polyline.setMap(null);
}
}