-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparks.html
184 lines (174 loc) · 8.26 KB
/
parks.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
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/misc.css">
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="js/leaflet/leaflet.css" />
<link rel="stylesheet" href="css/l.geosearch.css" />
<link rel="stylesheet" href="css/park-info.css" />
<title>NRV Park Search</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>
<body>
<div data-role="page" id="page2">
<div data-role="header">
<h1>Nearest Parks</h1>
<h3>Click to see the park on a map.</h3>
<input id="updateLocation" type="button" value="Update Location" onclick="updateLocation()" style="float:left;" />
</div>
<div role="main" class="ui-content">
<ul id='parkList' data-role="listview"></ul>
</div>
<div data-role="content" id="map" class="mapfix">
<div id='mapid' style='display:none;'></div>
</div>
</div>
<script src="https://npmcdn.com/[email protected]/dist/leaflet.js"></script>
<script src="js/l.control.geosearch.js"></script>
<script src="js/l.geosearch.provider.openstreetmap.js"></script>
<script src="js/l.geosearch.provider.esri.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.mobile-1.4.5.min.js"></script>
<script src="js/misc.js"></script>
<script type="text/javascript">
function detectRefresh() {
return performance.navigation.type == 1 && getUrlVars()["location"] == "on";
}
function updateLocation(map) {
$( "#parkList" ).listview({ }).empty();
geoLocation.getLocation().then(function(position) {
refreshResults(map, position.coords.longitude, position.coords.latitude);
}).fail(function(err) {
showError(err);
});
}
$('#page2').on('pagecreate', function() {
var map = createMap();
if(getUrlVars()["location"] == "on") {
if(detectRefresh())
{
updateLocation(map);
}
else
{
refreshResults(map, getUrlVars()["lon"], getUrlVars()["lat"]);
}
}
else
{
$('#updateLocation').closest('.ui-btn').hide();
var provider = new L.GeoSearch.Provider.OpenStreetMap();
var phrase = getUrlVars()["address"];
var retries = 0;
var searcher = new L.Control.GeoSearch({
provider: provider,
showPopup: true
});
searcher.addTo(map);
map.on('geosearch_error',function() {
if(retries == 0) {
retries++;
provider = new L.GeoSearch.Provider.Esri();
searcher.geosearch(phrase + " virginia");
} else {
alert("Could not find that address with any providers");
window.location = "index.html";
}
});
searcher.geosearch(phrase + " virginia");
}
});
function registerClickEvent(currentLocation)
{
$(document).on("click",".show-map",function(event){
window.location = "map.html?id="+$(this).attr('rel')+"&location="+getUrlVars()["location"]+"&lat="+currentLocation[1]+"&lon="+currentLocation[0];
});
}
function refreshResults(map, lon, lat)
{
$( "#parkList" ).listview({ }).empty();
registerClickEvent([lon, lat]);
var currentLocation = [lon, lat];
$.when(getData.getAmenities(), getData.getNearbyParks(currentLocation)).done(function(amentityData, parkData){
var allAmenities = {};
amentityData.map(function(amenity){
if(!(amenity.park_id in allAmenities)) {
allAmenities[amenity.park_id] = {};
}
allAmenities[amenity.park_id][amenity.amenity_type_id] = true;
});
console.log(allAmenities);
var amenities = [];
for(v in getUrlVars())
{
if(v.substring(0,2) === "cb") {
amenities.push(parseInt(v.substring(2, v.length)));
}
}
console.log(amenities);
parkData = parkData
.filter(function(park) {
for(var i; i < amenities.length; ++i) {
if(!(park.id_ in allAmenities) || !(amenities[i] in allAmenities[park.id_])) {
return false;
}
}
return true;
})
.sort(function(a,b) {
if(a.distance_ < b.distance_) {
return -1;
}
return 1;
});
if (parkData.length == 0) {
alert("Could not find any close parks fitting those criteria");
window.location = "index.html";
}
parkData.map(function(park){
console.log(park);
$("#parkList").append('<li><a href="#" class="show-map" data-transition="slide" rel="'+park.id_+'">'+park.park_name_+' ('+ park.distance_.toFixed(1) +' mi)</a></li>').listview('refresh');
});
});
}
function createMap(){
var customControl = L.Control.extend({
options: {
position: 'topleft'
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
container.innerHTML = '<button type="button" class="btn btn-primary">Back to Search</button>';
container.style.backgroundColor = 'white';
container.style.width = '100px';
container.style.height = '30px';
container.onclick = function(){
console.log('buttonClicked');
window.location = "index.html";
}
return container;
}
});
var map = L.map('mapid');
map.addControl(new customControl());
map.on('geosearch_showlocation', function(result) {
var currentLocation = [result.Location.X, result.Location.Y];
//getData.getParks(currentLocation);
refreshResults(map, currentLocation[0], currentLocation[1]);
});
return map;
}
</script>
</body>
</html>