Closed lisablunt closed 3 years ago
Hi @lisablunt! I'm happy and honored that my project in https://github.com/alulsh/military-brat-map was helpful for you!
I took a look at your code in https://github.com/lisablunt/lisablunt.github.io/blob/master/map/index.html and I've got something working for you! First, here's a GIF of it working locally on my computer for proof:
I am dropping two git diffs for index.html
and map.js
. I tried my best to preserve your code style and only make the bare minimum changes needed to get the map working again so that the changes I made are easy to spot.
I noticed in index.html
that you have two Mapbox GL JS map divs - one at the top with an id of map-init
and another after the body tag with an id of map
. This was causing the sidebar not to load. I changed the class for the first div from map-init
to map
and deleted the other map div on line 68. Once I did that the sidebar loaded again!
I also deleted all of the JavaScript code from lines 70 through 290 and moved it to map.js
.
For easier readability and maintainability, I also strongly recommend that you move the CSS in lines 56-66 into your styles.css
file.
Here is the full diff for the changes I made to index.html
in the details (click on the arrow to expand and view):
I noticed there was still some Mapbox.js code in lines 1-4 and line 220, so I removed that code (the L
in those lines of code refers to Leaflet, Mapbox.js is a Leaflet plugin). I then copied over your Mapbox GL JS code from index.html
to map.js
.
I created a new object called markerSource
that references your marker
object with all of the place GeoJSON data. You could optionally make this a single object like in this code if you prefer. Adding the layout
properties allows the symbols to display on the map. I also had to change all of the marker-symbol
properties to icon
in markers for this to work, similar to this Mapbox GL JS example that uses Maki icons.
For the fly to a location functionality, I replaced map.setView()
(an old Mapbox.js method) with the new flyTo()
method. I also switched from marker.openPopup()
(a Mapbox.js method) to new mapboxgl.Popup()
. map.on('click')
has a similar HTML pop up, you could use a function like this one to reduce duplication, but this is optional.
Here is the full diff in the details for my changes to map.js
:
' + description + '
') + .addTo(map); +}); +}); + function fly(lat, long, title) { - map.setView([lat, long], 7); - placesLayer.eachLayer(function(marker){ - if(marker.feature.properties.title === title) { - marker.openPopup(); + map.flyTo({ + center: [long, lat], + zoom: 7, + essential: true, + }); + markers.features.forEach(function(marker){ + if(marker.properties.title === title) { + new mapboxgl.Popup() + .setLngLat([long, lat]) + .setHTML('' + marker.properties.title + '' + marker.properties.description + '
') + .addTo(map); } }) } @@ -36,7 +90,7 @@ var markers = { 'type': 'Feature', 'properties': { 'title': 'Washington, DC', - 'marker-symbol': 'star', + 'icon': 'star', 'description': '1980s - PresentI noticed that your project is using base.css from Mapbox. This is an old CSS framework (see the 2013 blog post) that was replaced by Assembly.
I just updated https://github.com/alulsh/military-brat-map today to switch from Base to Assembly. You can check out the changes I made for the migration in https://github.com/alulsh/military-brat-map/compare/66d6f44..9370a5d. You're welcome to copy this code and switch to Assembly if you want! There is a lot of documentation and examples for Assembly (there isn't any for Base anymore) so it's a lot easier to work with Assembly.
@alulsh Infinite thanks, Alex! Really appreciate the time you took to help resolve my issue.
After merging '/map/index.html' with the Mapbox GL JS file '/map/map_GLJS.html', the sidebar navigation menu no longer appears. If the sidebar is rendered on the page, the links no longer correspond to their respective city coordinates and all popups are disabled:
Places I've Lived & Travelled map
The desired result is along the lines of @alulsh's map.