|
5 | 5 | <meta charset="UTF-8">
|
6 | 6 | <title>Type Ahead 👀</title>
|
7 | 7 | <link rel="stylesheet" href="style.css">
|
| 8 | + <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" |
| 9 | + integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" |
| 10 | + crossorigin="" /> |
| 11 | + <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" |
| 12 | + integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" |
| 13 | + crossorigin=""></script> |
8 | 14 | </head>
|
9 | 15 |
|
10 | 16 | <body>
|
| 17 | + <div id="mapid"></div> |
11 | 18 |
|
12 | 19 | <form class="search-form">
|
13 | 20 | <input type="text" class="search" placeholder="City or State" autofocus>
|
|
21 | 28 | let userLatitude = undefined;
|
22 | 29 | let userLongitude = undefined;
|
23 | 30 | const cities = [];
|
| 31 | + let matchesMarkers = []; |
24 | 32 |
|
| 33 | + console.log('about to ask'); |
25 | 34 | navigator.geolocation.getCurrentPosition((loc) => {
|
| 35 | + console.log('asked'); |
26 | 36 | userLatitude = loc.coords.latitude;
|
27 | 37 | userLongitude = loc.coords.longitude;
|
| 38 | + console.log('sorting by dist'); |
28 | 39 | sortByDistance();
|
29 | 40 | })
|
30 | 41 |
|
|
85 | 96 | `;
|
86 | 97 | }).join('');
|
87 | 98 | suggestions.innerHTML = html;
|
| 99 | + |
| 100 | + displayMarkers(matchArray); |
88 | 101 | }
|
89 | 102 |
|
90 | 103 | const searchInput = document.querySelector('.search');
|
91 | 104 | const suggestions = document.querySelector('.suggestions');
|
92 | 105 |
|
93 | 106 | searchInput.addEventListener('change', displayMatches);
|
94 | 107 | searchInput.addEventListener('keyup', displayMatches);
|
| 108 | + |
| 109 | + |
| 110 | + /* Map stuff */ |
| 111 | + var mymap = L.map('mapid').setView([39.833333, -98.583333], 4); |
| 112 | + |
| 113 | + L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoiYXN0cm91ZCIsImEiOiJja2N6ODE1b3MwZmNvMnVsNWxzZmhmMWk5In0.D-VAy85TlIn8MtmkLTt7aA', { |
| 114 | + maxZoom: 18, |
| 115 | + attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' + |
| 116 | + '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' + |
| 117 | + 'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>', |
| 118 | + id: 'mapbox/streets-v11', |
| 119 | + tileSize: 512, |
| 120 | + zoomOffset: -1 |
| 121 | + }).addTo(mymap); |
| 122 | + |
| 123 | + // Boss Headquarters |
| 124 | + const bosIcon = L.icon({ |
| 125 | + iconUrl: 'marker-bos-icon.png', |
| 126 | + iconSize: [50, 41], |
| 127 | + iconAnchor: [25, 20], |
| 128 | + popupAnchor: [-3, -76], |
| 129 | + shadowUrl: 'marker-shadow.png', |
| 130 | + shadowSize: [50, 41], |
| 131 | + shadowAnchor: [25, 20] |
| 132 | + }); |
| 133 | + const bossMarker = L.marker([45.3849302, -77.6854559], { icon: bosIcon }).addTo(mymap); |
| 134 | + |
| 135 | + function displayMarkers(matchArray) { |
| 136 | + // First clear existing markers |
| 137 | + matchesMarkers.forEach(marker => marker.remove()); |
| 138 | + matchesMarkers = []; |
| 139 | + |
| 140 | + // Place markers for the 100 closest matches |
| 141 | + matchArray.slice(0, 100).forEach(match => { |
| 142 | + matchesMarkers.push(L.marker([match.latitude, match.longitude]).addTo(mymap)); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
95 | 146 | </script>
|
96 | 147 | </body>
|
97 | 148 |
|
|
0 commit comments