-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.html
185 lines (166 loc) · 5.24 KB
/
main.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Constrution Fleet Planing App</title>
<link
href="https://api.tomtom.com/maps-sdk-for-web/cdn/6.x/6.16.0/maps/maps.css"
rel="stylesheet"
type="text/css"
/>
<script src="https://api.tomtom.com/maps-sdk-for-web/cdn/6.x/6.16.0/maps/maps-web.min.js"></script>
<script src="https://api.tomtom.com/maps-sdk-for-web/cdn/6.x/6.16.0/services/services-web.min.js"></script>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body::-webkit-scrollbar {
width: 0px;
height: 0px;
}
#map {
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<button class="show-history">Show History</button>
<button class="clear-markers">Clear Markers</button>
<div id="map" class="map"></div>
<script>
const API_KEY = "C7sHTlhiEDGA3l9kRNWfmeRhdICPGMis";
// const SanJose = { lon: 4.89707, lat: 52.377956 };
const SanJose = { lon: -121.867905, lat: 37.279518 };
var map = tt.map({
key: API_KEY,
container: "map",
center: SanJose,
zoom: 10,
});
map.addControl(new tt.FullscreenControl());
map.addControl(new tt.NavigationControl());
function displayFence() {
fetch(
"https://api.tomtom.com/geofencing/1/fences/ed451036-d527-40f3-836f-fd6844fc60af?key=gBuQtiHC80qAV541N23tKQYRUtZbAKIH"
)
.then((response) => response.json())
.then((result) => {
console.log(result);
map.addLayer({
id: "San Jose",
type: "fill",
source: {
type: "geojson",
data: result,
},
paint: {
"fill-color": "blue",
"fill-opacity": 0.6,
},
});
});
}
map.on("load", () => {
displayFence();
});
function sendPostion(lnglat) {
let cordinates = lnglat;
// create Json Object to store location
let position = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [cordinates.lng, cordinates.lat, 0],
},
object: "d789c800-5225-45bb-beda-520fc1cda79d",
// timestamp: "<YYYY-MM-DDThh:mm:ss>",
};
// Use fetch to send the location
fetch(
"https://api.tomtom.com/locationHistory/1/history/positions?key=gBuQtiHC80qAV541N23tKQYRUtZbAKIH",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(position),
}
).then((result) => {
console.log(result);
});
}
//markers array
const markers = [];
function addMarker(lnglat) {
const marker = new tt.Marker({ draggable: true })
.setLngLat(lnglat)
.addTo(map);
function onDragEnd() {
var lngLat = marker.getLngLat();
sendPostion(lngLat);
}
marker.on("dragend", onDragEnd);
markers.push(marker);
}
// get location history
function getObjectLocations() {
fetch(
"https://api.tomtom.com/locationHistory/1/history/positions/d789c800-5225-45bb-beda-520fc1cda79d?key=gBuQtiHC80qAV541N23tKQYRUtZbAKIH&adminKey=8QfxMVRZI6hkZjSRLeobFHEjsCMYlcnHLjQUR826aPXTaoMt&from=2021-11-16T00:00:00"
)
.then((response) => response.json())
.then((result) => {
console.log(result);
// console.log(result.positions.features[0].geometry.coordinates);
const lngLatArray = result.positions.features.map(
(feature) => feature.geometry.coordinates
);
console.log(lngLatArray);
lngLatArray.forEach((lngLat) => {
addMarker(lngLat);
});
// map.addLayer({
// id: "truck history",
// type: "line",
// source: {
// type: "geojson",
// data: {
// type: "Feature",
// geometry: {
// type: "LineString",
// coordinates: lngLatArray,
// },
// },
// },
// paint: {
// "line-color": "purple",
// "line-opacity": 0.7,
// "line-width": 8,
// },
// });
});
}
map.on("load", () => {
addMarker(SanJose);
});
// map.on("click", (e) => {
// sendPostion(e.lngLat);
// });
document.querySelector(".show-history").addEventListener("click", (e) => {
e.preventDefault();
getObjectLocations();
});
document
.querySelector(".clear-markers")
.addEventListener("click", (e) => {
e.preventDefault();
markers.forEach((marker) => marker.remove());
markers.length = 0;
});
</script>
</body>
</html>