|
| 1 | +--- |
| 2 | +layout: example |
| 3 | +category: example |
| 4 | +title: Play map locations as a slideshow |
| 5 | +description: 'Autoplay the locations of boroughs in New York City' |
| 6 | +tags: |
| 7 | + - animation |
| 8 | +--- |
| 9 | +<style> |
| 10 | +.map-overlay-container { |
| 11 | + position: absolute; |
| 12 | + width: 25%; |
| 13 | + top: 0; |
| 14 | + left: 0; |
| 15 | + padding: 10px; |
| 16 | + z-index: 1; |
| 17 | +} |
| 18 | + |
| 19 | +.map-overlay { |
| 20 | + font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif; |
| 21 | + background-color: #fff; |
| 22 | + border-radius: 3px; |
| 23 | + padding: 10px; |
| 24 | + box-shadow:0 1px 2px rgba(0,0,0,0.20); |
| 25 | +} |
| 26 | + |
| 27 | +.map-overlay h2, |
| 28 | +.map-overlay p { |
| 29 | + margin: 0 0 10px; |
| 30 | +} |
| 31 | +</style> |
| 32 | + |
| 33 | +<div id='map'></div> |
| 34 | + |
| 35 | +<div class='map-overlay-container'> |
| 36 | + <div class='map-overlay'> |
| 37 | + <h2 id='location-title'></h2> |
| 38 | + <p id='location-description'></p> |
| 39 | + <small>Text credit: <a target='_blank' href='http://www.nycgo.com/neighborhoods'>nycgo.com</a></small> |
| 40 | + </div> |
| 41 | +</div> |
| 42 | + |
| 43 | +<script> |
| 44 | +var map = new mapboxgl.Map({ |
| 45 | + container: 'map', |
| 46 | + style: 'mapbox://styles/mapbox/emerald-v8', |
| 47 | + center: [-74.0315, 40.6989], |
| 48 | + maxZoom: 16, |
| 49 | + minZoom: 9, |
| 50 | + zoom: 9.68 |
| 51 | +}); |
| 52 | + |
| 53 | +var title = document.getElementById('location-title'); |
| 54 | +var description = document.getElementById('location-description'); |
| 55 | + |
| 56 | +var locations = [{ |
| 57 | + "id": "2", |
| 58 | + "title": "The Bronx", |
| 59 | + "description": "This is where hip-hop was born, where the Yankees became a dynasty and where you can find New York City's leading zoo and botanical garden.", |
| 60 | + "camera": { |
| 61 | + center: [-73.8709, 40.8255], |
| 62 | + zoom: 12.21, |
| 63 | + pitch: 50 |
| 64 | + } |
| 65 | +}, { |
| 66 | + "id": "3", |
| 67 | + "title": "Brooklyn", |
| 68 | + "description": "No matter how hip it looks on TV, NYC's most populous borough is best experienced in person. Read on to find out about live music, Prospect Park, Nets basketball and more.", |
| 69 | + "camera": { |
| 70 | + center: [-73.9499, 40.6260], |
| 71 | + bearing: -8.9, |
| 72 | + zoom: 11.68 |
| 73 | + } |
| 74 | +}, { |
| 75 | + "id": "1", |
| 76 | + "title": "Manhattan", |
| 77 | + "description": "Even if you think you know Manhattan—its world-class museums, fine dining and unforgettable views—the borough always has something new and exciting in store.", |
| 78 | + "camera": { |
| 79 | + center: [-74.0070, 40.7437], |
| 80 | + bearing: 25.3, |
| 81 | + zoom: 11.5 |
| 82 | + } |
| 83 | +}, { |
| 84 | + "id": "4", |
| 85 | + "title": "Queens", |
| 86 | + "description": "Taste food from around the globe, watch Mets baseball and US Open tennis, see cutting-edge art and more in one of the world's most diverse places.", |
| 87 | + "camera": { |
| 88 | + center: [-73.8432, 40.6923], |
| 89 | + bearing: 36, |
| 90 | + zoom: 11.37 |
| 91 | + } |
| 92 | +}, { |
| 93 | + "id": "5", |
| 94 | + "title": "Staten Island", |
| 95 | + "description": "Take a free ferry ride to an island getaway filled with historic architecture, stunning views, gardens and many family-friendly attractions.", |
| 96 | + "camera": { |
| 97 | + center: [-74.1991, 40.5441], |
| 98 | + bearing: 28.4, |
| 99 | + zoom: 11.64 |
| 100 | + } |
| 101 | +}, { |
| 102 | + "title": "Boroughs of new york", |
| 103 | + "description": "New York City is made up of five boroughs: the Bronx, Brooklyn, Manhattan, Queens and Staten Island. Each one has enough attractions—and enough personality—to be a city all its own.", |
| 104 | + "camera": { |
| 105 | + center: [-74.0315, 40.6989], |
| 106 | + zoom: 9.68, |
| 107 | + bearing: 0, |
| 108 | + pitch: 0 |
| 109 | + } |
| 110 | +}]; |
| 111 | + |
| 112 | +function highlightBorough(code) { |
| 113 | + // Only show the polygon feature that cooresponds to `borocode` in the data |
| 114 | + map.setFilter('highlight', ["==", "borocode", code]); |
| 115 | +} |
| 116 | + |
| 117 | +function playback(index) { |
| 118 | + title.textContent = locations[index].title; |
| 119 | + description.textContent = locations[index].description; |
| 120 | + |
| 121 | + highlightBorough(locations[index].id ? locations[index].id : ''); |
| 122 | + |
| 123 | + // Animate the map position based on camera properties |
| 124 | + map.flyTo(locations[index].camera); |
| 125 | + |
| 126 | + map.once('moveend', function() { |
| 127 | + // Duration the slide is on screen after interaction |
| 128 | + window.setTimeout(function() { |
| 129 | + // Increment index |
| 130 | + index = (index + 1 === locations.length) ? 0 : index + 1; |
| 131 | + playback(index); |
| 132 | + }, 3000); // After callback, show the location for 3 seconds. |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +// Display the last title/description first |
| 137 | +title.textContent = locations[locations.length - 1].title; |
| 138 | +description.textContent = locations[locations.length - 1].description; |
| 139 | + |
| 140 | +map.on('load', function() { |
| 141 | + |
| 142 | + // Polygon fill for each borough of new york city. |
| 143 | + // Used to highlight when its borough is active. |
| 144 | + map.addSource("boroughs", { |
| 145 | + "type": "vector", |
| 146 | + "url": "mapbox://mapbox.8ibmsn6u" |
| 147 | + }); |
| 148 | + |
| 149 | + map.addLayer({ |
| 150 | + "id": "highlight", |
| 151 | + "type": "fill", |
| 152 | + "source": "boroughs", |
| 153 | + "source-layer": "original", |
| 154 | + "paint": { |
| 155 | + "fill-color": "#fd6b50", |
| 156 | + "fill-opacity": 0.25 |
| 157 | + }, |
| 158 | + "filter": ["==", "borocode", ""] |
| 159 | + }, 'neighborhood_small_label'); // Place polygon under the neighborhood labels. |
| 160 | + |
| 161 | + // Start the playback animation for each borough |
| 162 | + playback(0); |
| 163 | +}); |
| 164 | +</script> |
0 commit comments