37
loading...
This website collects cookies to deliver better user experience
npx create-react-app burger-map
cd burger-map
and then start the development server with yarn start
,http://localhost:3000
.App.js
safely remove all the stuff which is not needed the file will look like this:import './App.css';
function App() {
return (
<div className="App">
</div>
);
}
export default App;
axios
which will execute our HTTP requests. yarn add axios
App.js
import {useState, useEffect} from 'react';
const [loading, setLoading] = useState(true)
const [burgers, setBurgers] = useState(null)
axios
to handle the get-request. https://my-burger-api.herokuapp.com/burgers
.useEffect(() => {
const fetchBurgers = async () => {
const {data} = await axios.get("https://my-burger-api.herokuapp.com/burgers")
setBurgers(data)
setLoading(false)
}
fetchBurgers()
}, []);
const
in your application.const MAPBOX_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
console.log(data)
[
{
addressId: 0
country: "United Kingdom"
line1: "Brick Lane"
line2: "Shoreditch"
number: "104"
postcode: "E1 6RL"
},
...
]
latitude
and longitude
values to display the data on the correct position we have to call the geocoding endpoint. Here we have to pass the address object as a string seperated with ampersands. const getLatLong = async (address) => {
const {line1, line2, number, postcode, country} = address;
let searchKey = [line1, line2, number, postcode, country].filter(Boolean).join().replaceAll(" ", "&");
const {data: {features}} = await axios.get(`https://api.mapbox.com/geocoding/v5/mapbox.places/${searchKey}.json?access_token=${MAPBOX_TOKEN}`)
return features[0].center;
}
fetchBurgers
function to look like that:useEffect(() => {
const fetchBurgers = async () => {
let burgers = []
const {data} = await axios.get("https://my-burger-api.herokuapp.com/burgers")
for (const burger of data) {
burger.latLong = await getLatLong(burger.addresses[0])
burgers.push(burger)
}
setBurgers(burgers)
setLoading(false)
}
fetchBurgers()
}, []);
latLong
which is an array containing the lat and long values.yarn add react-map-gl
import ReactMapGL, {Marker} from 'react-map-gl';
if (loading) {
return 'loading burgers..'
}
return (
<div className="App">
<div>
<h1 style={{textAlign: 'center'}}>World Map</h1>
</div>
<ReactMapGL
width="100vw"
height="100vh"
mapboxApiAccessToken={MAPBOX_TOKEN}
>
</ReactMapGL>
</div>
);
const [vieport, setVieport] = useState({})
<ReactMapGL
{...vieport}
width="100vw"
height="100vh"
onViewportChange={nextVieport => setVieport(nextVieport)}
mapboxApiAccessToken={MAPBOX_TOKEN}
>
</ReactMapGL>
renderBurgers
function and call inside of the ReactMapGL component.X
for each burger.function renderBurgers() {
return burgers.map(burger => (
<Marker
key={burger.id}
latitude={burger.latLong[1]}
longitude={burger.latLong[0]}
>
X
</Marker>
))
}
renderBurgers
function in the ReactMapGl Component. So let's add it:<ReactMapGL
{...vieport}
width="100vw"
height="100vh"
onViewportChange={nextVieport => setVieport(nextVieport)}
mapboxApiAccessToken={MAPBOX_TOKEN}
>
{renderBurgers()}
</ReactMapGL>