-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.php
76 lines (66 loc) · 2.47 KB
/
map.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Peta Lokasi</title>
<link
rel="stylesheet"
href="https://unpkg.com/[email protected]/dist/leaflet.css"
/>
<style>
#map {
height: 600px;
width: 100%;
}
</style>
</head>
<body>
<?php
require_once('koneksi.php');
// Ambil data lokasi dari database
$sql = "SELECT id, img_dir, latitude, longitude FROM images";
$result = $conn->query($sql); $locations = array(); if ($result->num_rows >
0) { while($row = $result->fetch_assoc()) { $locations[] = array( 'id' =>
$row['id'], 'img_dir' => $row['img_dir'], 'latitude' => $row['latitude'],
'longitude' => $row['longitude'] ); } } $conn->close(); ?>
<h1>Peta Lokasi</h1>
<label for="photoSelect">Pilih Foto:</label>
<select id="photoSelect">
<?php foreach ($locations as $location): ?>
<option value="<?php echo $location['id']; ?>">
Foto ID:
<?php echo $location['id']; ?>
</option>
<?php endforeach; ?>
</select>
<div id="map"></div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script>
const locations = <?php echo json_encode($locations); ?>;
// Inisialisasi peta
const map = L.map('map').setView([0, 0], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
const photoSelect = document.getElementById('photoSelect');
photoSelect.addEventListener('change', () => {
const selectedId = photoSelect.value;
const selectedLocation = locations.find(location => location.id == selectedId);
if (selectedLocation) {
const lat = parseFloat(selectedLocation.latitude);
const lon = parseFloat(selectedLocation.longitude);
const imgDir = selectedLocation.img_dir;
map.setView([lat, lon], 13);
L.marker([lat, lon]).addTo(map)
.bindPopup(`<img src="${imgDir}" alt="Foto" style="width: 100px; height: auto;">`)
.openPopup();
}
});
if (locations.length > 0) {
photoSelect.value = locations[0].id;
photoSelect.dispatchEvent(new Event('change'));
}
</script>
</body>
</html>