/
NearbyPosts.js
100 lines (86 loc) 路 2.28 KB
/
NearbyPosts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import React, { useState, useEffect } from "react";
import axios from "axios";
import NotFound from "./NotFound";
import PageLoader from "./PageLoader";
import Post from "./Post";
import mapboxgl from "mapbox-gl";
import { useParams } from "react-router-dom";
const NearbyPosts = (props) => {
const { center } = useParams();
const [posts, setPosts] = useState([]);
const [loading, setLoading] = useState(true);
const [locationName, setLocationName] = useState("");
let [lng, lat] = center.split(",");
const fetchPosts = async () => {
setLoading(true);
try {
const url = `/api/v1/posts/near?lat=${lat}&lng=${lng}`;
const res = await axios.get(url);
setPosts(res.data.data.posts);
setLocationName(res.data.data.location.name);
setLoading(false);
} catch (e) {
setPosts([]);
setLoading(false);
}
};
useEffect(() => {
fetchPosts();
// MapBox Token
mapboxgl.accessToken =
"pk.eyJ1Ijoic21hbmFuMyIsImEiOiJja2FqcHNqZmMwY3k0MnRtbmUyaTA3c3k0In0.7AdKPQUF4xS4_WlTI1eO6Q";
// Render map on screen
var map = new mapboxgl.Map({
container: "map-container",
style: "mapbox://styles/mapbox/streets-v11",
center: [lng * 1, lat * 1],
zoom: 13,
});
// Location markers JSON File
const geojson = {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [lng * 1, lat * 1],
},
properties: {
title: "Mapbox",
description: locationName,
},
},
],
};
geojson.features.forEach(function (marker) {
// create a HTML element for each feature
var el = document.createElement("div");
el.className = "marker";
// make a marker for each feature and add to the map
new mapboxgl.Marker(el).setLngLat(marker.geometry.coordinates).addTo(map);
});
}, []);
return (
<div className="my-timeline">
<div className="nearby-posts__heading" id="map-container"></div>
{!loading ? (
<>
<div className="location-name">
<p>{locationName}</p>
</div>
{!!posts.length ? (
posts.map((post, index) => <Post post={post} key={post._id} />)
) : (
<div className="no-posts">
<NotFound message={"Nothing Here."} />
</div>
)}
</>
) : (
<PageLoader />
)}
</div>
);
};
export default NearbyPosts;