-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
141 lines (137 loc) · 4.65 KB
/
script.js
File metadata and controls
141 lines (137 loc) · 4.65 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict'
//constants needed throughout the program
const trailUrl = 'https://www.trailrunproject.com/data/get-trails';
const trailKey = '200417972-9dc9a277ef8cb3c883310ed069aec132';
const geoCodeUrl = 'https://maps.googleapis.com/maps/api/geocode/json';
const googleApi = 'AIzaSyDg4-ORDFbZJ9J7LASnL5qecmn78pHB3mo';
let searchLat = '';
let searchLong = '';
let resultsLoc = {};
function buildResults(trails) {
let trailLength = '';
let trailName = '';
let trailSummary = '';
let trailAscent = '';
let condStatus = '';
let condDetails = '';
let condDate = '';
let trailStars = '';
//set returned data
$('.results').append(`<h1>${trails.length} trails found:</h1>`);
for (let i = 0; i < trails.length; i++) {
let trailPic = trails[i].imgSmallMed;
//placeholder pic if trail does not have an image.
if (trailPic === '') {
trailPic = 'no-trail-pic.jpg';
}
trailLength = trails[i].length;
trailName = trails[i].name;
trailSummary = trails[i].summary;
trailAscent = trails[i].ascent;
trailStars = trails[i].stars;
resultsLoc[i] = {
name: `${trailName}`,
latitude: `${trails[i].latitude}`,
longitude: `${trails[i].longitude}`,
zInd: (i + 1)
};
condStatus = trails[i].conditionStatus;
condDetails = trails[i].conditionDetails;
condDate = trails[i].conditionDate;
//If the trail status have never been set, set the condition details to space, and the date to Never
if (condDetails == null) {
condDetails = '';
condDate = 'Never';
}
let resultsList = `
<div class="js-single-result">
<p class="js-name">${trailName}</p>
<p>${trailSummary}</p>
<p>Length: ${trailLength} miles, Ascent: ${trailAscent}</p>
<p>Stars: ${trailStars}</p>
<p>Condition: ${condStatus}, Date Reported: ${condDate}</p>
<p>${condDetails}</p>
</div>
<div class="js-img-container">
<img class="js-trail-image" src="${trailPic}" alt="picture of trailhead">
</div>
<div style="clear: both;"></div>`;
$('.results').append(resultsList);
}
//compose all results into an interactive map
let resultsNum = trails.length;
initMap(resultsNum);
}
function initMap(resultsNum) {
if (searchLat != '') {
let map, lastInd;
map = new google.maps.Map(document.getElementById('map'), {
zoom: 9.5,
center: {
lat: searchLat,
lng: searchLong
},
});
//build each marker
for (let i = 0; i < resultsNum; i++) {
let marker = new google.maps.Marker({
position: {
lat: parseFloat(resultsLoc[i].latitude),
lng: parseFloat(resultsLoc[i].longitude)
},
map: map,
title: resultsLoc[i].name,
zIndex: resultsLoc[i].zInd,
});
// Attaches an info window to a marker with the provided message. When the
// marker is clicked, the info window will open with the trail data
//if an infowindow is open, it will close when another marker is clicked.
let infowindow = new google.maps.InfoWindow({
content: `${resultsLoc[i].name} <br/><a href="https://www.google.com/maps/dir/?api=1&destination=${marker.position}">Get Directions</a>`
});
marker.addListener('click', function() {
if (lastInd) {
lastInd.close();
}
infowindow.open(map, marker);
lastInd = infowindow;
});
}
//display the map
$('#map').removeClass('hidden');
}
}
function getTrails(lat, long) {
//format the location and url for Trail Run API
let latLong = `lat=${lat}&lon=${long}`;
searchLat = lat;
searchLong = long;
const searchRadius = $('#in-radius').val();
let url2 = trailUrl + '?' + latLong + '&maxDistance=' + searchRadius + '&key=' + trailKey;
fetch(url2).then(resp => resp.json()).then(respJson => {
//call function to loop through the results and push them to the DOM.
buildResults(respJson.trails);
}).catch(error => alert(`something went wrong: ${error.message}`));
}
function getLatLong(searchLocation) {
//format the search location into latitude and longitude by using the Geocode API
searchLocation = '?address=' + searchLocation.replace(' ', '+');
let url = geoCodeUrl + searchLocation + '&key=' + googleApi;
fetch(url).then(response => response.json()).then(responseJson => {
let lat = responseJson.results[0].geometry.location.lat;
let long = responseJson.results[0].geometry.location.lng;
//now we need to get the trail data.
getTrails(lat, long);
}).catch(err => alert(`something went wrong: ${err.message}`));
}
//When the form is submitted, retrieve the entered location value, and clear any previous results
function handleForm() {
$('.search-form').submit(event => {
event.preventDefault();
let searchLocation = $('#in-city').val();
$('.map').empty();
$('.results').empty();
getLatLong(searchLocation);
});
}
$(handleForm());