-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added bike station data
- Loading branch information
Showing
3 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as d3 from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'; | ||
import mapboxgl from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'; | ||
|
||
console.log("Mapbox GL JS Loaded:", mapboxgl); | ||
|
@@ -15,12 +16,12 @@ const map = new mapboxgl.Map({ | |
maxZoom: 18 // Maximum allowed zoom | ||
}); | ||
|
||
map.on('load', async () => { | ||
map.on('load', async () => { | ||
map.addSource('boston_route', { | ||
type: 'geojson', | ||
data: 'https://bostonopendata-boston.opendata.arcgis.com/datasets/boston::existing-bike-network-2022.geojson' | ||
}); | ||
|
||
map.addLayer({ | ||
id: 'bike-lanes', | ||
type: 'line', | ||
|
@@ -42,9 +43,48 @@ map.on('load', async () => { | |
type: 'line', | ||
source: 'cambridge_route', | ||
paint: { | ||
'line-color': '#CC5500', // Corrected color code | ||
'line-color': '#CC5500', | ||
'line-width': 2.5, | ||
'line-opacity': 0.6 | ||
} | ||
}); | ||
}); | ||
|
||
const jsonurl = 'https://dsc106.com/labs/lab07/data/bluebikes-stations.json'; | ||
const jsonData = await d3.json(jsonurl); | ||
|
||
let stations = jsonData.data.stations; | ||
console.log('Stations Array:', stations); | ||
|
||
const svg = d3.select('#map').select('svg'); | ||
|
||
function getCoords(station) { | ||
const point = new mapboxgl.LngLat(+station.lon, +station.lat); | ||
const { x, y } = map.project(point); | ||
return { cx: x, cy: y }; | ||
} | ||
|
||
const circles = svg.selectAll('circle') | ||
.data(stations) | ||
.enter() | ||
.append('circle') | ||
.attr('r', 5) | ||
.attr('fill', 'steelblue') | ||
.attr('stroke', 'white') | ||
.attr('stroke-width', 1) | ||
.attr('opacity', 0.8); | ||
|
||
function updatePositions() { | ||
circles | ||
.attr('cx', d => getCoords(d).cx) | ||
.attr('cy', d => getCoords(d).cy); | ||
} | ||
|
||
updatePositions(); | ||
|
||
map.on('move', updatePositions); | ||
map.on('zoom', updatePositions); | ||
map.on('resize', updatePositions); | ||
map.on('moveend', updatePositions); | ||
}); | ||
|
||
|