Untitled

 avatar
unknown
javascript
a year ago
1.8 kB
2
Indexable
<script>
    var locations = [
        <?php foreach ($lokasi as $a): ?> {
                name: '<?= $a['nama'] ?>',
                lat: <?= $a['lat'] ?>,
                lng: <?= $a['long'] ?>,
                titik: '<?= $a['lokasi'] ?>',
            },
        <?php endforeach; ?>
    ]

    var map = L.map('map').setView([-3.999553627975692, 122.51220190723], 12);

    var greenIcon = L.icon({
        iconUrl: '<?= base_url('leaflet/images/marker.png') ?>',
        iconSize: [25, 41],
        iconAnchor: [12, 41],
        popupAnchor: [1, -34]
    });

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

    var selectedLocation = null; // Menyimpan lokasi yang dipilih

    var markers = L.layerGroup(); // Membuat grup layer untuk semua marker

    locations.forEach(function (location) {
        var latlng = L.latLng(location.lat, location.lng);
        var marker = L.marker(latlng);
        marker.bindPopup(`
            <b>${location.titik}</b>
            </br>
            <p class="m-0">Lat : ${location.lat}</p>
            <p class="m-0">Long : ${location.lng}</p>
            `);

        if (location.name == '<?= $alat['nama'] ?>') {
            selectedLocation = latlng;
            marker.setIcon(greenIcon);
        }

        markers.addLayer(marker); // Menambahkan marker ke grup layer
    });

    map.addLayer(markers); // Menambahkan grup layer ke peta

    if (selectedLocation) {
        // Jika ada lokasi yang dipilih, set peta ke lokasi tersebut
        map.setView(selectedLocation, 15);
    }

    // Membuka semua popup secara bersamaan
    markers.openPopup();
Editor is loading...
Leave a Comment