-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
187 lines (161 loc) · 4.97 KB
/
main.js
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
// Creates an array of all the Trees with Name, Long, and Lat attributes
console.log("Importing Tree Data");
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "18.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
x = xmlDoc.getElementsByTagName("ThemeEntityAbridgedData");
const Trees = []; // Array of all the trees
for (i = 0; i < x.length; i++)
{
TreeName = x[i].childNodes[1].childNodes[0].nodeValue;
try
{
TreeImage = x[i].childNodes[0].childNodes[0].nodeValue;
LatLong = x[i].childNodes[3].childNodes[0].nodeValue;
LatLong = LatLong.split("\"");
const Tree = {Name:TreeName, Lat:LatLong[3], Long:LatLong[7], Image:TreeImage}; // Creating the Tree object
Trees.push(Tree); // Adding the Tree object to the Trees array
}
catch{} // This skips trees that don't have a long/lat
}
// Creating the map
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([-91.64, 44.048]),
zoom: 17
})
});
TreeFeatures = [];
for (i = 0; i < Trees.length; i++) // Adds all coordinates to the features array
{
var lon = parseFloat(Trees[i]['Long']);
var lat = parseFloat(Trees[i]['Lat']);
const iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat])),
name: Trees[i]['Name'],
});
const TreeColors = ['#8fbb09', '#d6e715', '#fbf601', '#fbcd26', '#fa8f04', '#f64d0d'] // Fall color hex codes
const iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
color: TreeColors[Math.floor(Math.random()*TreeColors.length)], //picks a random tree color
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'tree.png',
scale: 0.1,
}),
});
iconFeature.setStyle(iconStyle);
TreeFeatures.push(iconFeature)
}
const vectorSource = new ol.source.Vector({
features: TreeFeatures,
});
const vectorLayer = new ol.layer.Vector({
source: vectorSource,
});
map.addLayer(vectorLayer); // Display the layer with all the trees
// function showPosition(position)
// {
// var layer = new ol.layer.Vector({
// source: new ol.source.Vector({
// features: [
// new ol.Feature({
// geometry: new ol.geom.Point(ol.proj.fromLonLat([position.coords.longitude, position.coords.latitude]))
// })]
// })
// });
// map.addLayer(layer);
// }
function focusTree(Tree) // Used to focus the map onto a single tree
{
TreeFeatures = [];
const iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([Tree['Long'], Tree['Lat']])),
name: "current",
});
const iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'tree.png',
scale: 0.18,
}),
});
iconFeature.setStyle(iconStyle);
TreeFeatures.push(iconFeature)
const vectorSource = new ol.source.Vector({
features: TreeFeatures,
});
const vectorLayer = new ol.layer.Vector({
source: vectorSource,
});
map.addLayer(vectorLayer); // Display the layer with all the trees
map.getView().setCenter(ol.proj.fromLonLat([Tree['Long'], Tree['Lat']]));
map.getView().setZoom(19);
}
TreeIndex = -1;
getNextTree(); // Runs on startup to get the first tree
function getNextTree() // Gets the next tree in the array
{
var div = document.getElementById("bottom");
if (div.style.height == "100%")
{
div.style.height = "20%";
div.style.top = "80%";
}
//discuss in comments
//could print over prvious tree but that doesnt allow us to make it smaller
//do you think you could make this in the focus tree?
//we cant pass it to the getNextTree because focustree does call anything
//probs, anything is possiblee
TreeIndex = TreeIndex += 1;
console.log(TreeIndex);
document.getElementById(
"MainTourText").innerHTML = Trees[TreeIndex]['Name'] +
"<br><button id = \"BackButton\" onclick=\"decreaseTreeIndex()\">Back</button>" +
"<button id = \"HereButton\" onclick=\"displayTreeInfo()\">I'm Here</button>" +
"<button id = \"NextButton\" onclick=\"getNextTree()\">Next Tree</button>";
focusTree(Trees[TreeIndex]);
}
function displayTreeInfo()
{
console.log(TreeIndex);
var div = document.getElementById("bottom");
if(div)
{
div.style.height = "100%";
div.style.top = "0%";
ImageUrl = Trees[TreeIndex]['Image'];
console.log(ImageUrl);
document.getElementById(
"MainTourText").innerHTML=
"<p id=\"TreeNameText\">" + Trees[TreeIndex]['Name'] + "</p>" +
"<img id=\"TreeImg\" src=\"" + ImageUrl + "\">" +
"<p id=\"TreeInfoText\">this tree is epic.</p>" +
"<br><button id = \"NextButton\" onclick=\"getNextTree()\">Next Tree</button>";
}
}
// Used for the back button
function decreaseTreeIndex()
{
if (TreeIndex <= 0)
{
alert("No Previous Trees");
return
}
TreeIndex = TreeIndex - 2;
getNextTree();
}
function increaseTreeIndex()
{
TreeIndex += 1;
}