forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlantMap.js
More file actions
130 lines (117 loc) 路 4.33 KB
/
Copy pathPlantMap.js
File metadata and controls
130 lines (117 loc) 路 4.33 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useEffect, useState } from 'react';
import { MapContainer, TileLayer, Marker, Popup, useMap } from 'react-leaflet';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
// Fix per i marker di Leaflet
delete L.Icon.Default.prototype._getIconUrl;
L.Icon.Default.mergeOptions({
iconRetinaUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png',
iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png',
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png',
});
// Icona personalizzata per le piante
const plantIcon = new L.Icon({
iconUrl: '/plant-icon.svg',
iconSize: [32, 32],
iconAnchor: [16, 32],
popupAnchor: [0, -32],
});
// Componente per centrare la mappa
const MapController = ({ plants }) => {
const map = useMap();
useEffect(() => {
if (plants && plants.length > 0) {
const bounds = plants
.filter(p => p.gps && p.gps.lat && p.gps.lng)
.map(p => [p.gps.lat, p.gps.lng]);
if (bounds.length > 0) {
map.fitBounds(bounds, { padding: [50, 50] });
}
}
}, [map, plants]);
return null;
};
const PlantMap = ({ plants = [], onPlantClick }) => {
const [selectedPlant, setSelectedPlant] = useState(null);
const handleMarkerClick = (plant) => {
setSelectedPlant(plant);
if (onPlantClick) onPlantClick(plant);
};
return (
<div style={{ width: '100%', height: '600px' }}>
<MapContainer
center={[0, 0]}
zoom={2}
style={{ height: '100%', width: '100%' }}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
/>
<MapController plants={plants} />
{plants.map((plant) => {
if (!plant.gps || !plant.gps.lat || !plant.gps.lng) return null;
return (
<Marker
key={plant._id || plant.id}
position={[plant.gps.lat, plant.gps.lng]}
icon={plantIcon}
eventHandlers={{
click: () => handleMarkerClick(plant)
}}
>
<Popup>
<div style={{ maxWidth: '200px' }}>
<h3 style={{ margin: '0 0 8px 0' }}>
{plant.commonName || plant.species || 'Unknown Plant'}
</h3>
<p style={{ margin: '4px 0' }}>
<strong>Species:</strong> {plant.species || 'Unknown'}
</p>
{plant.commonName && (
<p style={{ margin: '4px 0' }}>
<strong>Common Name:</strong> {plant.commonName}
</p>
)}
{plant.age && (
<p style={{ margin: '4px 0' }}>
<strong>Age:</strong> {plant.age} years
</p>
)}
{plant.size && (
<p style={{ margin: '4px 0' }}>
<strong>Size:</strong> {plant.size}
</p>
)}
<p style={{ margin: '4px 0' }}>
<strong>Status:</strong>{' '}
<span style={{
color: plant.status === 'verified' ? 'green' :
plant.status === 'pending' ? 'orange' : 'red'
}}>
{plant.status || 'pending'}
</span>
</p>
{plant.photos && plant.photos.length > 0 && (
<img
src={plant.photos[0]}
alt={plant.species}
style={{ width: '100%', maxHeight: '100px', objectFit: 'cover', borderRadius: '4px' }}
/>
)}
<button
onClick={() => window.open(`/plants/${plant._id}`, '_blank')}
style={{ marginTop: '8px', padding: '4px 12px', cursor: 'pointer' }}
>
View Details
</button>
</div>
</Popup>
</Marker>
);
})}
</MapContainer>
</div>
);
};
export default PlantMap;